Reputation: 25
I have this animation that complete the strokeEnd of a defined path, and I want to change the path before letting the animation continues. In other words, the animation should go like this:
path1 animation=>change path1 to path2 =>path2 animation =>change path2 to path1=>path1 animation (thus repeating forever)
On the above diagram, the path1 and path2 animation are the same animation writing like this:
let strokeAnimation = CABasicAnimation(keyPath: "strokeEnd")
strokeAnimation.duration = 1
strokeAnimation.fromValue = 0
strokeAnimation.toValue = 1
strokeAnimation.repeatCount = HUGE
strokeAnimation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
How should I achieve that?
Upvotes: 1
Views: 196
Reputation: 25
I figured this out by using CADisplayLink
displayLink = CADisplayLink(target: self, selector: Selector("updatePathForLoadingLayer"))
displayLink?.frameInterval = 60
displayLink?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
By setting the frameInterval to 60, the refreshing rate of the screen would be once per second and thus the path of the CAShapeLayer would be handled in the fun "updatePathForLoadingLayer".
Later, in "stopAnimation" function, I remove set nil to displayLink by stopping the refreshing.
displayLink?.removeFromRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
displayLink = nil
Hope this helps to those who need it.
P.S. Any improvement would be appreciated.
Upvotes: 1