Swift-animate CAshapeLayer stroke color

情到浓时终转凉″ 提交于 2019-12-22 12:20:44

问题


I'm trying to find a way to animate the color of the stroke that I am creating

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)

What I want to achieve is: while it's being created (I'm creating it over a duration of 1 seconds), the color will animate itself


回答1:


Found out the solution. Enjoy. After you create the CAShapeLayer of course :

    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")



回答2:


Roi Mulia's answer for Swift 3.0 and 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")


来源:https://stackoverflow.com/questions/26602171/swift-animate-cashapelayer-stroke-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!