简体   繁体   English

UIView动画和UIButton停止按b / c正常工作-迅速4

[英]UIView Animation and UIButton stop working b/c Home pressed - Swift 4

I have a playButton that performs a "breathing animation". 我有一个执行“呼吸动画”的playButton The button works just fine when I press it. 当我按下该按钮时,它的效果很好。 The problem occurs if I press the device's Home Button and then re-open the app. 如果我按设备的“主页”按钮,然后重新打开应用程序,则会出现问题。 Upon re-opening, the playButton does not have the "breathing animation" and it does not work (nothing happens when it is pressed). 重新打开后, playButton没有“呼吸动画”,并且不起作用(按下时什么也没有发生)。

@IBOutlet weak var playButton: UIButton!

override func viewWillAppear(_ animated: Bool) {

    UIView.animate(withDuration: 1.0,
                   delay: 0,
                   options: [.autoreverse, .repeat, .allowUserInteraction],
                   animations: {
                    self.playButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)
    },
                   completion: nil

    )

 }

I've dealt with this issue in a previous game app where I needed to save and pause the game if the user pressed the Home Button or if there was an interruption (incoming call). 我已经在以前的游戏应用程序中处理过此问题,如果用户按下“主页”按钮或出现中断(来电),我需要保存并暂停游戏。 So, I am well aware of: 因此,我很清楚:

func applicationDidBecomeActive() {}

func applicationWillResignActive() {}

func applicationDidEnterBackground() {}

But, I am not dealing with a gameState, timer, the need to save data, etc. I simply want my button and its animation to work properly when the app re-opens after the Home Button is pressed. 但是,我没有处理gameState,计时器,需要保存数据等问题。我只是希望在按下Home按钮后重新打开应用程序时,我的按钮及其动画可以正常工作。

I also tried using override func viewDidLayoutSubviews() {} instead of viewWillAppear . 我也尝试使用override func viewDidLayoutSubviews() {}代替viewWillAppear But that did not work. 但这没有用。

First of all, if you have a multiple animations within the same ViewController (VC) that occur after playButton is pressed, then that may explain why the it is becoming disabled upon return from background. 首先,如果在同一个ViewController (VC)中有多个动画,这些动画在按下playButton之后发生,那么这可以解释为什么从背景返回时将其禁用的原因。 Why? 为什么? I don't know. 我不知道。 But I had a similar issue and resolved it by creating a new class and VC for the multiple animations that were originally set to occur when my UIButton was pressed. 但是我遇到了类似的问题,并通过为最初设置为在按下UIButton时发生的多个动画创建新的class和VC来解决了该问题。 Inside of my button's IBAction , I simply created a segue to then new VC. 在按钮的IBAction ,我只是为新的VC创建了一个序列。

With regards to the animation, you could approach this two ways: 1) Pause and Resume the animation using CALayer OR 2) Simply use NotificationCenter without even having to touch any AppDelegate code. 关于动画,您可以采用以下两种方法:1)使用CALayer暂停和恢复动画;或2)只需使用NotificationCenter,甚至无需触摸任何AppDelegate代码。 I prefer simple ways b/c it will save time and effort. 我更喜欢简单的方法,因为它可以节省时间和精力。 So, try this code in the VC where the button animation is to occur: 因此,请在将出现按钮动画的VC中尝试以下代码:

override func viewWillAppear(_ animated: Bool) {

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

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



    UIView.animate(withDuration: 1.0,
                    delay: 0,
                    options: [.autoreverse, .repeat, .allowUserInteraction],
                    animations: {
                        self.playButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)
                    },
                    completion: nil)
}

@objc func goingToBackground(){
    playButton.transform = .identity
}

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM