Animation stops after segueing to different view controller

最后都变了- 提交于 2020-01-04 01:49:13

问题


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.


回答1:


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

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        appDevButton.layer.removeAllAnimations()
    }



回答2:


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()
    }


来源:https://stackoverflow.com/questions/29868374/animation-stops-after-segueing-to-different-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!