gpt4 book ai didi

ios - CAShapeLayer 未在动画中更新

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:49 27 4
gpt4 key购买 nike

我有一个包含基本形状的 CAShapeLayer,它是在“drawRect”方法期间创建的,如下所示。

override func drawRect(frame: CGRect)
{
let rectangleRect = CGRectMake(frame.minX + 2.5, frame.minY + 2.5, frame.width - 5, frame.height - 5)
let rectanglePath = UIBezierPath(roundedRect: rectangleRect, cornerRadius: 4)
shapeLayer.path = rectanglePath.CGPath

if (self.active == true)
{
shapeLayer.fillColor = selectedColor.CGColor
} else
{
shapeLayer.fillColor = buttonColor.CGColor
}

shapeLayer.strokeColor = buttonOutlineColor.CGColor
shapeLayer.lineWidth = 5.0

self.layer.addSublayer(shapeLayer)

}

我用它来创建一个带有笔触的简单正方形。然后我有另一种方法,它有一个 CAShapeLayer 的动画。但是,调用该方法时,CAShapeLayer 没有任何变化。动画方法如下所示。

let animation = CABasicAnimation(keyPath: "strokeColor")
animation.duration = 0.5
animation.repeatCount = 10
animation.fromValue = UIColor.whiteColor()
animation.toValue = UIColor.redColor()
animation.autoreverses = true
shapeLayer.addAnimation(animation, forKey: "strokeColor")

变量 shapeLayer 已在类的顶部定义。我对为什么这不起作用很感兴趣。非常感谢任何有关如何解决此问题的建议!

最佳答案

这是一个困扰我每次使用Quartz/CoreGraphics的问题。 fromValue 和 toValue 需要 CGColor 参数,而不是 UIColor

只需将其更改为 UIColor.whiteColor().CGColorUIColor.redColor().CGColor

一般来说,如果您使用前缀 CACG,您将要使用 CGColor 等人

关于ios - CAShapeLayer 未在动画中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com