簡體   English   中英

CAShapeLayer未在動畫中更新

[英]CAShapeLayer Not Updating in Animation

我有一個包含基本形狀的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

通常,如果使用前綴CACGCGColor要使用CGColor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM