2015-04-22 88 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