Reputation: 793
I'm fairly new to Swift still. I'm wondering how I can terminate an animation function in swift outside the execution of the function. In the case below I run an animation for 15 seconds but anytime a user presses a certain button I want the animation to stop and the elements to disappear. Any thoughts on the best way to go about this? Here is the function.
func createTimerAnimation () {
// create timer line shapes
guard self.timerisActive else {
print("in guard 1")
return
}
let screenWidth = (self.view.bounds.width)
let screenHeight = (self.view.bounds.height)
let timerShapeLeft = drawTimerShape()
let timerShapeRight = drawTimerShape()
let timerShapeCenter = drawTimerShape()
view.addSubview(timerShapeLeft)
view.addSubview(timerShapeRight)
view.addSubview(timerShapeCenter)
UIView.animate(withDuration: 14.5, delay: 0.0, options: [.curveEaseOut], animations:{
timerShapeLeft.frame = CGRect (x: (screenWidth / 2), y: (screenHeight - 60), width: timerShapeLeft.frame.width, height: (timerShapeLeft.frame.height - 30))
timerShapeRight.frame = CGRect (x: (screenWidth / 2), y: (screenHeight - 60), width: timerShapeRight.frame.width, height: (timerShapeRight.frame.height - 30))
timerShapeCenter.frame = CGRect (x: (screenWidth / 2), y: (screenHeight - 60), width: timerShapeCenter.frame.width, height: (timerShapeCenter.frame.height))
timerShapeLeft.alpha = 1.0
timerShapeRight.alpha = 1.0
timerShapeCenter.alpha = 0.4
timerShapeLeft.tintColor = self.hexGray
timerShapeRight.tintColor = self.hexGray
guard self.timerisActive else {
print("in guard 1")
return
}
Upvotes: 0
Views: 117
Reputation: 16327
Animations are actually handled by the view's layer so you can cancel them there with CALayer.removeAllAnimations() :
view.layer.removeAllAnimations()
Upvotes: 2