2
我试图获得一个非常基本的CAGradientLayer
动画。我有一个基本的2色CAGradientLayer
设置并添加到我的UIViewController
。CAGradientLayer颜色更改动画未执行
// Set up the gradient layer.
var gradientLayer: CAGradientLayer! {
let layer = CAGradientLayer()
layer.frame = self.view.bounds
layer.colors = [UIColor.redColor().CGColor, UIColor.yellowColor().CGColor]
layer.locations = [0.0, 1.0]
return layer
}
我想用下面的代码来制作动画:
@IBAction func changeBackground(sender: AnyObject) {
let oldColors = self.gradientLayer.colors
let newColors = [UIColor.purpleColor().CGColor, UIColor.blueColor().CGColor]
self.gradientLayer.colors = newColors
let animation: CABasicAnimation = CABasicAnimation(keyPath: "colors")
animation.fromValue = oldColors
animation.toValue = newColors
animation.duration = 0.3
animation.removedOnCompletion = true
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.delegate = self
self.gradientLayer.addAnimation(animation, forKey: "animateGradientColorChange")
}
但是,此代码不会产生在CAGradientLayer
任何变化。我在这里做错了什么?