当我按下主页按钮然后重新启动应用程序时,我的动画停止运行。设置按钮停止旋转并且闪烁标签刚刚消失。这里是我的两个动画代码:当主页按钮被按下并且应用程序再次重新启动时,动画无法工作
闪烁动画:
extension UILabel {
func startBlink() {
UIView.animate(withDuration: 0.8,
delay:0.0,
options:[.autoreverse, .repeat],
animations: {
self.alpha = 0
}, completion: nil)
}
}
旋转动画:
extension UIButton {
func startRotating() {
UIView.animate(withDuration: 4.0, delay: 0.0, options:[.autoreverse, .repeat,UIViewAnimationOptions.allowUserInteraction], animations: {
self.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}, completion: nil)
}
}
当我运行它:
override func viewDidLoad() {
super.viewDidLoad()
settingsButton.layer.cornerRadius = 0.5 * settingsButton.bounds.size.width
settingsButton.clipsToBounds = true
settingsButton.imageView?.contentMode = .scaleAspectFit
NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
func appMovedToForeground() {
tapToPlayLabel.startBlink()
settingsButton.startRotating()
print("DID")
}
添加CALayerAnimation你试图用'didBecomeActive'通知,而不是'willEnterForeground'?我假设动画可能正在工作,但由于在'willEnterForeground'上调用了代码,所以您没有看到动画发生 – Malik
@Malik您应该添加您的评论作为答案 – klinger
我假设它工作。感谢您的高举。只是发布它作为答案 – Malik