【问题标题】:UIButton becomes Inanimate after the user presses Home Button用户按下 Home 按钮后 UIButton 变为 Inanimate
【发布时间】:2019-02-22 03:22:24
【问题描述】:

我有一个名为findButtonUIButton,它有一个脉动或呼吸动画。在模拟器和设备中测试应用程序时,一切正常,直到我在动画发生时单击主页按钮。如果我按下主页按钮然后重新打开应用程序,动画将不再出现。

我尝试了在这个网站上找到的各种答案,但都没有奏效。我尝试过使用ExtensionsNotificationCenter,但过去应该对人们有用的东西对我有用。下面是在按下 Home 键之前完美运行的动画代码:

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

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

更新:通过移动按下后出现的动画,我可以让“findButton”在按下时工作。我创建了一个新的 VC 和 class,并将带有点击后动画的 findButton IBAction 放在那里。

不幸的是,findButton 从后台返回后仍然没有生命。我希望NoticationCenter 能够工作,因为我将点击后动画从第一个 VC 中移出。

【问题讨论】:

  • 首先在viewWillAppear里面调用super.viewWillAppear
  • 请不要提出可以免于关闭的问题。有人可能会发现您错过的潜在重复,如果发生这种情况,读者希望您调查建议的链接。
  • 先生。 Nainwal,当我提出您的建议时,收到此错误 Expression resolves to an unused function

标签: ios swift uibutton uiviewanimation notificationcenter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以尝试注册应用程序确实进入后台

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

    你可以看到一个工作示例here

    【讨论】:

    • pooja,感谢您的建议。我以前试过这个,没有成功。但是,我认为它现在可以工作了,因为我将其他一些动画从 VC 中移出,并在按下时让 findButton 工作。请参阅更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2020-02-27
    • 2012-01-20
    相关资源
    最近更新 更多