The solution omz suggested worked lags when I tried to revitalize the property, so I continued to search. I found a great solution - to use the Quartz Core framework and a shell called CADisplayLink. βThis class is specifically designed for animations, whereβ your data is likely to change in every frame. βIt tries to send a message to the target every time something is redrawn on the screen.β Source
And a library that works that way.
Edit: But there is a more efficient solution. For animating a mask that applies to the image layer. I do not know why I did not do this from the very beginning. CABasicAnimation is faster than any custom drawing. Here is my implementation:
class ProgressBar: UIView { var imageView:UIImageView! var maskLayer: CAShapeLayer! var currentValue: CGFloat = 1 override init(frame: CGRect) { super.init(frame: frame) updateUI() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) updateUI() } func updateUI(){ makeGradient() setUpMask() } func animateCircle(strokeEnd: CGFloat) { let oldStrokeEnd = maskLayer.strokeEnd maskLayer.strokeEnd = strokeEnd
By the way, if you want to improve the animation, I suggest the excellent book "IOS Core Animation: Advanced Techniques Nick Lockwood Book"
source share