【发布时间】:2015-06-30 09:48:24
【问题描述】:
我正在尝试使用 CAKeyFrameAnimation 为 CALayer 的 backgroundColor 设置动画。
当我用经典颜色制作它时,它会起作用;但是用patternImageUIColor(patternImage:"imageName").CGColor生成的Color,根本就不行。
@IBAction func animFirstView(sender: AnyObject)
{
addAnim(view1, colors: [UIColor.blackColor().CGColor, UIColor.redColor().CGColor]) // THAT WORKS
}
@IBAction func animSecondView(sender: AnyObject)
{
var firstColor = UIColor(patternImage: UIImage(named:"stars1")!).CGColor;
addAnim(view2,
colors: [firstColor]); // THAT doesn't works at all :(
}
func addAnim(view:UIView, colors:[CGColor])
{
let anim = CAKeyframeAnimation(keyPath:"backgroundColor")
anim.values = colors;
anim.keyTimes = [0.0, 0.25, 0.5, 0.75, 1];
anim.calculationMode = kCAAnimationDiscrete
anim.duration = 0.3;
anim.repeatCount = Float.infinity;
view.layer.addAnimation(anim, forKey: "backgroundColor");
}
有人有想法吗?
难道不能这样做还是我做错了什么?
【问题讨论】:
-
感谢您在 github 上发布示例!这给了我一些可以工作的东西。
标签: ios swift core-animation cakeyframeanimation cgcolor