Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation stops after segueing to different view controller

I have an animation in my app that basically just makes a UIButton grow and shrink to make it obvious to the user that they should tap.

The problem is that while it works fine when the view first appears, it doesn't work if I go to a different view controller (with a segue) and then return (nothing happens).

Here is my code:

override func viewWillAppear(animated: Bool) {
    expandAnimation()
}

func expandAnimation() {
    var animation = CABasicAnimation(keyPath: "transform.scale")
    animation.toValue = NSNumber(float: 0.9)
    animation.duration = 1
    animation.repeatCount = 100
    animation.autoreverses = true
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    appDevButton.layer.addAnimation(animation, forKey: nil)
}

I'm sure it's a simple fix, but I couldn't find any info online.

like image 453
user3746428 Avatar asked Apr 25 '15 17:04

user3746428


1 Answers

Remove the animation from the button when you leave the view,

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        appDevButton.layer.removeAllAnimations()
    }
like image 128
rdelmar Avatar answered Sep 21 '22 00:09

rdelmar