用户按Home按钮后,Uibutton变得无生命



我有一个UIButton,称为findButton,具有脉动或呼吸动画。在模拟器和设备中测试该应用程序时,一切都可以正常工作,直到动画发生时单击主按钮为止。如果我按下主按钮然后重新打开应用程序,则动画不再发生。

我尝试了该网站上发现的各种答案,但没有任何作用。我已经尝试使用ExtensionsNotificationCenter,但是据说过去对我有用的东西对我有用。这是在按下主页按钮之前完美工作的动画代码:

override func viewWillAppear(_ animated: Bool) {
    UIView.animate(withDuration: 1.0,
                   delay: 0,
                   options: [.autoreverse, .repeat, .allowUserInteraction],
                   animations: {
                    self.findButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)}, completion: nil)
} 

我想知道也许我是否需要在动画进入后台之前暂停该动画?

update :通过移动按下动画发生的动画,我能够使'FindButton'工作。我创建了一个新的vc和 class,并用 findButton ibaction用点击动画。

不幸的是,从背景返回后,findButton仍然无生命。我希望NoticationCenter现在将点击点击动画从第一个VC移出。

@pooja的答案肯定会带您走正确的道路。但是,如果您使用的是iOS 12,Xcode 10,Swift 4.2,则在实现NotificationCenter时会遇到一些错误。尝试 @pooja的代码,但要进行这些更改:

NotificationCenter.default.addObserver(self, selector:#selector(doSomethingBefore), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: UIApplication.willEnterForegroundNotification, object: nil)

您可以尝试注册应用程序确实输入背景

 NotificationCenter.default.addObserver(self, selector:#selector(doSomethingBefore), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

注册应用将成为活动

NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)

视图消失时设置按钮的转换

  @objc func doSomethingBefore(){
        findButton.transform = .identity
    }

启动动画当应用活动

 @objc func doSomething(){
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0,
                       delay: 0.3,
                       options: [.autoreverse, .repeat, .allowUserInteraction],
                       animations: {
                        self.findButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)}, completion: nil)
        self.view.layoutIfNeeded()
    }

您可以在此处看到一个有效的样本

最新更新