Swift-animate CAshapeLayer笔触颜色

时间:2014-10-28 06:32:42

标签: swift uiviewanimation cashapelayer

我试图找到一种动画我正在创建的笔画颜色的方法

circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
circleLayer.lineCap =  kCALineCapRound
circleLayer.fillColor = UIColor.clearColor().CGColor
CABasicAnimation fill = CABasicAnimation.
circleLayer.strokeColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5).CGColor
circleLayer.lineWidth = 20.0;
circleLayer.lineJoin = kCALineJoinRound
// Don't draw the circle initially
circleLayer.strokeEnd = 0.0

// Add the circleLayer to the view's layer's sublayers
layer.addSublayer(circleLayer)

我想要达到的目标是:在创建它的过程中(我在1秒钟内创建它),颜色会自动生成

2 个答案:

答案 0 :(得分:8)

找到解决方案。请享用。在创建CAShapeLayer之后:

    let animcolor = CABasicAnimation(keyPath: "strokeColor")
    animcolor.fromValue         = UIColor.greenColor().CGColor
    animcolor.toValue           = UIColor.orangeColor().CGColor
    animcolor.duration          = 1.0;
    animcolor.repeatCount       = 0;
    animcolor.autoreverses      = true
    circleLayer.addAnimation(animcolor, forKey: "strokeColor")

答案 1 :(得分:1)

Roi Mulia对Swift 3.0和Swift 4.0的回答

    let animcolor = CABasicAnimation(keyPath: "strokeColor")
    animcolor.fromValue         = UIColor.green.cgColor
    animcolor.toValue           = UIColor.orange.cgColor
    animcolor.duration          = 8.0;
    animcolor.repeatCount       = 0;
    animcolor.autoreverses      = true
    shapeLayer.add(animcolor, forKey: "strokeColor")