2

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

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • 3
    Posting two questions in one is generally not a good idea. Question 1 is clearly "opinion based" and therefore off-topic at this site. – I would suggest that you restrict your question to problem 2 and try to explain the problem better. – Martin R Oct 28 '14 at 06:37
  • Hint: the first question is opinion-based; stick to the second one. – Drux Oct 28 '14 at 06:41

2 Answers2

12

Updated to Swift 5.

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

    let animcolor = CABasicAnimation(keyPath: "strokeColor")
    animcolor.fromValue         = UIColor.green.cgColor
    animcolor.toValue           = UIColor.orange.cgColor
    animcolor.duration          = 1.0
    animcolor.repeatCount       = 0
    animcolor.autoreverses      = true
    myLayer.add(animcolor, forKey: "strokeColor")
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
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")
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36