在Swift中动画CAGradientLayer [英] Animate CAGradientLayer in Swift
问题描述
我正在尝试使用Swift为CAGradientLayer设置动画。对于资源我正在使用此发布<这是我的代码
class ViewController:UIViewController {
var gradient:CAGradientLayer?;
覆盖func viewDidLoad(){
super.viewDidLoad()
}
覆盖func viewDidAppear(动画: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:kCAMediaTimin) gFunctionLinear)
animation.delegate = self
self.gradient?.addAnimation(animation,forKey:animateGradient)
}
}
我在Objective-C中尝试了这个并且它可以工作,但是我需要在Swift中使用它并且当我运行程序时它动画但最后回到之前的颜色,我需要它,所以保持蓝色。
我检查了你的代码在这里发现了一个错误。你忘了设置 self.gradient
的颜色你只是为动画添加颜色,这就是为什么动画工作正常,最后你会看到它的颜色回到红色
。
写下这个遗失的代码:
var toColors:[AnyObject] = [UIColor.blueColor()。CGColor,UIColor.blueColor()。CGColor]
self.gradient.colors = toColors //你错过了这一行
var animation:CABasicAnimation = CABasicAnimation(keyPath:colors)
您还可以在此处查看工作代码: 示例
更新:如何重复此渐变持续过渡?
所以你可以使用委托
CAAnimation
。它会在动画完成后通知您,然后您可以设置 fromColors
和 toColors
值并调用 animateLayer()
一次又一次地运作。但为此,您必须对代码进行一些更改。
- 从
和 toColors
global。 - 在
animationDidStop
委托方法中更改其值。 - 再次调用
animateLayer()
函数。
这是委托
方法正文:
覆盖func animationDidStop(动画:CAAnimation!,已完成标志:Bool){
self.toColors = self.fromColors;
self.fromColors = self.gradient?.colors
animateLayer()
}
您还可以在此处查看工作代码:示例
I am attemping to animate the CAGradientLayer using Swift. For a resource I'm using this post
Here is my code
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")
}
}
I've attempted this in Objective-C and it works, but I need to use it in Swift and when I run the program it animates but goes back to the previous color at the end, I need it so remain blue.
I have checked your code and found one mistake here. You forgot to set color of self.gradient
you are just giving colors to animation thats why animation is working fine and at the end you see its color goes back to red
.
Write this missing code:
var toColors: [AnyObject] = [UIColor.blueColor().CGColor, UIColor.blueColor().CGColor]
self.gradient.colors = toColors // You missed this line
var animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
You can also check working code here: Sample
UPDATE: how do I repeat this gradient transition continuously ?
So you can use delegate
of CAAnimation
. It will let you know once the animation has been finished and then you can set the fromColors
and toColors
value and call the animateLayer()
function again and again. But for this you will have to make few changes in your code.
- Make
fromColors
andtoColors
global. - Change their value in
animationDidStop
delegate method. - Call the
animateLayer()
function again.
Here is the delegate
method body:
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.toColors = self.fromColors;
self.fromColors = self.gradient?.colors
animateLayer()
}
And you can also check working code here: Sample
这篇关于在Swift中动画CAGradientLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!