转到不同的视图控制器后动画停止

Animation stops after segueing to different view controller

我的应用程序中有一个动画,它基本上只是让 UIButton 变大和变小,让用户明白他们应该点击。

问题是,虽然它在视图首次出现时工作正常,但如果我转到另一个视图控制器(使用 segue)然后 return(没有任何反应),它就不起作用。

这是我的代码:

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

我确定这是一个简单的修复,但我在网上找不到任何信息。

离开视图时移除按钮的动画,

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

尝试解决方案:

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