好吧,我在使用CAGradientLayer快速制作的动画颜色渐变时出现问题 -
动画来自这两个颜色数组:
Self.gradient.colors = [colorWithHexString("#4342F9").CGColor, colorWithHexString("#A442F9").CGColor, UIColor.magentaColor().CGColor] //start
self.toColors = [colorWithHexString("#FB22B5").CGColor, colorWithHexString("#FB258F").CGColor,colorWithHexString("#FD5E50").CGColor,colorWithHexString("#FD963C").CGColor] //end
渐变动画效果很好,直到动画结束并需要循环。这是完整的动画:
override func viewDidAppear(animated: Bool) {
self.gradient = CAGradientLayer()
self.gradient.frame = self.view.bounds
self.gradient.colors = [colorWithHexString("#4342F9").CGColor, colorWithHexString("#A442F9").CGColor, UIColor.magentaColor().CGColor] //start
self.toColors = [colorWithHexString("#FB22B5").CGColor, colorWithHexString("#FB258F").CGColor,colorWithHexString("#FD5E50").CGColor,colorWithHexString("#FD963C").CGColor] //end
//self.gradient.colors = [colorWithHexString("#6442F9").CGColor, colorWithHexString("#6442F9").CGColor]
//self.toColors = [colorWithHexString("#FB863F").CGColor, colorWithHexString("#FB863F").CGColor]
self.view.layer.insertSublayer(self.gradient, atIndex: 0)
animateLayer()
}
func animateLayer(){
self.fromColors = self.gradient.colors
self.gradient.colors = self.toColors! as! [AnyObject] // You missed this line
var animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
animation.delegate = self
animation.fromValue = fromColors
animation.toValue = toColors
animation.duration = 10.00
animation.removedOnCompletion = true
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.delegate = self
self.gradient.addAnimation(animation, forKey:"animateGradient")
}
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.toColors = self.fromColors;
self.fromColors = self.gradient.colors
animateLayer()
}
完成循环后,渐变“跳过”意味着它不会从较暗的渐变颜色渐变到较浅的渐变颜色,因为它会反向旋转。它只是“翻转”。
为什么这样做?如何在animationDidStop中循环后使其淡出?