I have the following animation. This works well however if image.flash is called in an if statement or a loop, the animation starts over and looks very unsmooth.
I need a way to allow the first call to the flash function to continue until stopped. even if the function is called subsequent times.
extension UIView {
func flash() {
self.alpha = 0;
UIView.animate(withDuration: 0.5, delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in self?.alpha = 1.0 })
}
func stopFlash() {
layer.removeAllAnimations()
alpha = 0
}
}
CodePudding user response:
Check if view is already in the middle of animation, to don't repeat your animation like using guard so before you add new animation remove previous ones.
func flash() {
// check if self is in the middle of view or containing an specific of animation
guard self.layer.animationKeys() == nil else { return }
self.alpha = 0;
UIView.animate(withDuration: 0.5, delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in
self?.alpha = 1.0
print(self?.layer.animationKeys()) // print type of animation
})
}