在Swift中动画CAGradientLayer

DrP*_*nce 17 objective-c ios swift

我正在尝试使用Swift为CAGradientLayer设置动画.对于我正在使用这篇文章的资源

这是我的代码

class ViewController: UIViewController {

  var gradient : CAGradientLayer?;

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func viewDidAppear(animated: Bool) {

    self.gradient = CAGradientLayer()
    self.gradient?.frame = self.view.bounds
    self.gradient?.colors = [ UIColor.redColor().CGColor, UIColor.redColor().CGColor]
    self.view.layer.insertSublayer(self.gradient, atIndex: 0)

    animateLayer()
  }

  func animateLayer(){

    var fromColors = self.gradient?.colors
    var toColors: [AnyObject] = [ UIColor.blueColor().CGColor, UIColor.blueColor().CGColor]

    var animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")

    animation.fromValue = fromColors
    animation.toValue = toColors
    animation.duration = 3.00
    animation.removedOnCompletion = true
    animation.fillMode = kCAFillModeForwards
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animation.delegate = self

    self.gradient?.addAnimation(animation, forKey:"animateGradient")
  }
}
Run Code Online (Sandbox Code Playgroud)

我在Objective-C中尝试了这个并且它可以工作,但我需要在Swift中使用它,当我运行程序时它会动画但最后回到之前的颜色,我需要它,所以保持蓝色.

The*_*ger 27

我检查了你的代码,发现这里有一个错误.你忘了设置颜色,self.gradient你只是给动画颜色,这就是为什么动画工作正常,最后你看到它的颜色可以追溯到red.

写下这个遗漏的代码:

var toColors: [AnyObject] = [UIColor.blueColor().CGColor, UIColor.blueColor().CGColor]
self.gradient.colors = toColors // You missed this line
var animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
Run Code Online (Sandbox Code Playgroud)

您还可以在此处查看工作代码:示例

更新:如何连续重复此渐变过渡?

所以,你可以使用delegateCAAnimation.它会在动画完成后通知您,然后您可以设置fromColorstoColors值并反复调用该animateLayer()函数.但为此,您必须在代码中进行一些更改.

  • 制造fromColorstoColors全球化.
  • animationDidStop委托方法中更改它们的值.
  • animateLayer()再次调用该功能.

这是delegate方法体:

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {

    self.toColors = self.fromColors;
    self.fromColors = self.gradient?.colors
    animateLayer()
}
Run Code Online (Sandbox Code Playgroud)

您还可以在此处查看工作代码:示例

  • 请注意,动画对象具有`repeatCount`和`autoreverses`属性,可以很好地处理重复,而无需代理或计时器. (3认同)

Ech*_*lon 6

对于那些在评论中询问无尽动画的人,这是我的无尽动画背景的库存示例代码:-

完整的讨论和结果可以在我的博客上看到

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // create the gradient layer
        let gradient = CAGradientLayer()
        gradient.frame = self.view.bounds
        gradient.startPoint = CGPoint(x:0.0, y:0.5)
        gradient.endPoint = CGPoint(x:1.0, y:0.5)
        gradient.colors = [UIColor.red.cgColor, UIColor.green.cgColor]
        gradient.locations =  [-0.5, 1.5]

        let animation = CABasicAnimation(keyPath: "colors")
        animation.fromValue = [UIColor.red.cgColor, UIColor.green.cgColor]
        animation.toValue = [UIColor.green.cgColor, UIColor.red.cgColor]
        animation.duration = 5.0
        animation.autoreverses = true
        animation.repeatCount = Float.infinity

        // add the animation to the gradient
        gradient.add(animation, forKey: nil)

        // add the gradient to the view
        self.view.layer.addSublayer(gradient)
    }
 }
Run Code Online (Sandbox Code Playgroud)