Reputation: 3202
I have a CAShapeLayer
and trying to animate few properties. I did use CABasicAnimation
in the beginning, but since this loops over and over, I need the most officient way to do it.
Folowing code animates properly:
private func animateDidBecomeStraight () {
CATransaction.flush()
CATransaction.begin()
CATransaction.setAnimationDuration(0.2)
self.myShape.fillColor = UIColor.Red.cgColor
self.myShape.lineWidth = 1
self.myShape.strokeColor = UIColorRed.cgColor
CATransaction.commit()
}
private func animateDidBecomeUnStraight () {
CATransaction.begin()
CATransaction.setAnimationDuration(0.5)
self.myShape.fillColor = UIColor(white: 1, alpha: 1).cgColor
self.myShape.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
self.myShape.lineWidth = 0.1
CATransaction.commit()
}
Strait and Unstraight gets called in order and never in repetition.
First time, the animations work with proper durations. When Strait called first time and when Unstraight called first time. After that, they seem to lose animation duration. Changes seem to be instant.
Upvotes: -1
Views: 287
Reputation: 535945
CATransaction is not how to do this. Create two CABasicAnimation objects, one for straight and one for unstraight. Join them together in a CAAnimationGroup. Now configure the CAAnimationGroup as repeating.
Upvotes: 1