我刚刚构建了一个带有简单动画的 iOS 应用程序。但我在 UIViewPropertyAnimator 上苦苦挣扎。我想为一个按钮制作动画,这在我离开应用程序(按下主页按钮)并返回之前效果很好。动画已停止,不会重新开始。我试图在 ViewController didBecomeActive 之后停止动画并重新启动它,但这也不起作用。
我在 viewDidAppear 方法中启动动画如下:
var animator: UIViewPropertyAnimator!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification,object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeInActive), name: UIApplication.willResignActiveNotification,object: nil)
//Start Animation
animator = UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1, delay: 0, options: [.autoreverse, .repeat], animations: {
UIView.setAnimationRepeatAutoreverses(true)
UIView.setAnimationRepeatCount(1000)
self.scanButton.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}, completion: nil)
animator.startAnimation()
}
这里是我停止并重新启动动画的代码:
@objc func applicationDidBecomeActive() {
print("Active")
animator.startAnimation()
}
@objc func applicationDidBecomeInActive() {
print("InActive")
animator.stopAnimation(true)
animator.finishAnimation(at: .current)
}
我希望你们知道如何解决这个问题。提前致谢。