Animation stops after segueing to different view controller
Asked Answered
S

2

6

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.

Scrope answered 25/4, 2015 at 17:21 Comment(2)
Don't forget to call super in viewWillAppear:.Deteriorate
Doesn't seem to make any difference...Scrope
D
8

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

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        appDevButton.layer.removeAllAnimations()
    }
Deteriorate answered 25/4, 2015 at 17:37 Comment(3)
That did fix it, but I also had to change it from viewWillAppear() to viewDidAppear(). Thanks!Scrope
@Scrope I tried this but it did not seem to work... did you do anything differently then stated above? currently I have the method being called in the viewDidAppear method with [super viewDidAppear:YES]; , and the same for viewDidDisappear. Not sure why it isnt working..Frictional
I had a similar issue, and resolved it simply by calling animation() in viewDidAppear() without removeAllAnimations() in viewDidDisappear. Is this bad practice?Repeal
N
1

Try Solution:

    // Allows the animation to appear on View Controller
    override func viewWillAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        // Function call
        expandAnimation()
    }

    // Allows the animation to disappear from View Controller 
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(true)

        // Function call
        expandAnimation()
    }
Nakia answered 19/5, 2017 at 5:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.