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.

user3746428
  • 10,497
  • 19
  • 72
  • 129

2 Answers2

7

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

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        appDevButton.layer.removeAllAnimations()
    }
rdelmar
  • 102,832
  • 11
  • 203
  • 218
  • That did fix it, but I also had to change it from `viewWillAppear()` to `viewDidAppear()`. Thanks! – user3746428 Apr 25 '15 at 17:44
  • @user3746428 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.. – Will Von Ullrich Sep 28 '15 at 17:33
  • I had a similar issue, and resolved it simply by calling `animation()` in `viewDidAppear()` **without** `removeAllAnimations()` in `viewDidDisappear`. Is this bad practice? – Chameleon Mar 14 '17 at 05:14
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()
    }
Zeus
  • 357
  • 1
  • 6
  • 15