【问题标题】:CAKeyFrameAnimation with Color from patternImage带有来自 patternImage 的颜色的 CAKeyFrameAnimation
【发布时间】: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");

}

有人有想法吗?

难道不能这样做还是我做错了什么?

“bug”https://github.com/lastMove/patternBugDemo的测试用例

【问题讨论】:

  • 感谢您在 github 上发布示例!这给了我一些可以工作的东西。

标签: ios swift core-animation cakeyframeanimation cgcolor


【解决方案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);
}

编辑:这是另一种解决方法:继续使用图层背景颜色,但使用计时器直接更改它:

@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
}

【讨论】:

  • 非常感谢您。但我真的需要在颜色上做。在我的项目中,我将 SVG 加载到 CAShapeLayer 中,我可以使用 shapeLayer.fillColor = UIColor(patternImage:"image").CGColor 轻松地将图案图像放入那些 shapeLayers 中。现在我想用像 gif 这样的多个图像来做同样的事情。所以我不认为我可以使用 content 属性。除非我在主层上添加另一个带有蒙版的子层,但它看起来很重。我一定会创建一个错误报告和/或一张票。
  • 我认为你对“重”是完全错误的。就渲染服务器而言,反复更改backgroundColor 或反复更改contents(可能是另一层)并没有或多或少的工作!我同意您的错误报告想法,但如果我们找不到其他方法,我认为您应该继续使用此解决方法。
  • 我觉得很重的不是动画。每次添加一个子层+一个蒙版的事实。我将尝试使用填充颜色应用您的解决方法。 fillColor 的优点是它只填充图层的路径。所以我必须找到如何用你的方式模仿它。
  • 顺便说一句,您是否注意到在您的项目中,它不仅不适用于backgroundColor 作为一种模式,而且项目实际上挂起?点击按钮时请注意。如果您使用纯色,按钮会变为灰色,然后变回蓝色。但如果你做图案,按钮会变成灰色并停留在那里。这告诉我,我们正在导致整个渲染服务器挂起。
  • 我很同情,但我真的认为你过早地优化了。如果有问题,请尝试查看。我一直使用图层/蒙版,有时它们中的很多,彼此重叠。如果出现问题,当渲染服务器帧速率变慢时,您会“卡顿”(尤其是在设备上进行测试时);这可能发生,例如滚动单元格过于繁重的表格时。但如果只是坐在那里,我相信不会有任何问题。
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 2020-11-27
  • 1970-01-01
相关资源
最近更新 更多