2015-04-22 87 views
2

我正在尝试使用CAKeyFrameAnimation为CALayer的backgroundColor创建动画。 当我用它的古典色彩它的作品;但是从patternImage UIColor(patternImage:"imageName").CGColor生成的颜色完全不起作用。CAKeyFrameAnimation with colorImageImage

@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"); 

} 

有人有想法吗?

是不是可以这样做还是我做错了什么?

的“错误” https://github.com/lastMove/patternBugDemo

+0

感谢您在github上发布示例!这给了我一些工作。 – matt

回答

1

我不知道为什么backgroundColor作为关键帧动画不能正常工作的一部分设置为图案颜色的测试用例。我解决了这个模仿my own existing example在哪里设置contents为关键帧动画:

@IBAction func animSecondView(sender: AnyObject) 
{ 
    var firstColor = UIColor(patternImage: UIImage(named:"stars1")!) 
    var secondColor = UIColor(patternImage: UIImage(named:"stars2")!) 
    addAnim(view2, 
     colors: [firstColor, secondColor, firstColor, secondColor]); 
} 

func addAnim(view:UIView, colors:[UIColor]) 
{ 
    let anim = CAKeyframeAnimation(keyPath:"contents") 
    anim.values = colors.map { 
     col in 
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0) 
     col.setFill() 
     UIBezierPath(rect: view.bounds).fill() 
     let im = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return im.CGImage 
    } 
    anim.keyTimes = [0.0, 0.25, 0.75, 1.0]; 
    anim.calculationMode = kCAAnimationDiscrete 
    anim.duration = 1; 
    anim.repeatCount = Float.infinity; 
    view.layer.addAnimation(anim, forKey: nil); 
} 

enter image description here

编辑:这里的另一种解决办法:继续使用层背景颜色,而是直接更改它,用定时器:

@IBOutlet weak var view2: UIView! 
var colors = [UIColor]() 
var timer : NSTimer! 
var second = false 

@IBAction func animSecondView(sender: AnyObject) 
{ 
    var firstColor = UIColor(patternImage: UIImage(named:"stars1")!) 
    var secondColor = UIColor(patternImage: UIImage(named:"stars2")!) 
    self.colors = [firstColor, secondColor] 
    if let timer = self.timer { 
     timer.invalidate() 
    } 
    self.timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "anim:", userInfo: nil, repeats: true) 
} 
func anim(t:NSTimer) { 
    view2.layer.backgroundColor = self.colors[self.second ? 1 : 0].CGColor 
    self.second = !self.second 
} 
+0

非常感谢你。但我真的需要用颜色来做。 在我的项目中,我将SVG加载到CAShapeLayer中,我可以通过使用 shapeLayer.fillColor = UIColor(patternImage:“image”)。CGColor轻松地将图案图像放入这些shapeLayer中。 现在我想像GIF一样使用多个图像。 所以我不认为我可以使用内容属性。除非我在主层上添加一个带有掩膜的子层,但看起来很重。 我一定会创建一个bug报告和/或一张票。 – LastMove

+0

我认为你对“沉重”完全错了。就渲染服务器而言,重复更改'backgroundColor'或反复更改'contents'(可能是另一层)对于渲染服务器来说没有多少作用!我同意你的错误报告的想法,但我认为你应该继续前进,如果我们找不到其他方法,就使用这种解决方法。 – matt

+0

对我来说似乎很沉重的事情不是动画。事实上每次添加一个子图层+一个蒙版。 我将尝试将填充颜色应用于您的解决方法。 fillColor的优点是它只填充图层的路径。所以我必须找到如何用你的方式来模仿。 – LastMove