UIView动画和UIButton停止按b/c正常工作-迅速4 [英] UIView Animation and UIButton stop working b/c Home pressed - Swift 4

查看:108
本文介绍了UIView动画和UIButton停止按b/c正常工作-迅速4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行呼吸动画"的playButton.当我按下该按钮时,它的功能就很好.如果我按设备的主页"按钮,然后重新打开应用程序,则会出现问题.重新打开时,playButton没有呼吸动画",并且不起作用(按下时什么也没有发生).

I have a playButton that performs a "breathing animation". 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).

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

我也尝试使用override func viewDidLayoutSubviews() {}代替viewWillAppear.但这是行不通的.

I also tried using override func viewDidLayoutSubviews() {} instead of viewWillAppear. But that did not work.

推荐答案

首先,如果在同一ViewController(VC)中有多个动画,这些动画在按下playButton之后发生,那么这可以解释为什么从后台返回后,该功能将被禁用.为什么?我不知道.但是我遇到了类似的问题,并为最初设置为在按下UIButton时发生的多个动画创建了新的class和VC,从而解决了该问题.在按钮的IBAction内,我只是为随后的新VC创建了segue.

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. 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. Inside of my button's IBAction, I simply created a segue to then new VC.

关于动画,您可以采用以下两种方法:1)使用CALayer暂停和恢复动画,或者2)只需使用NotificationCenter,甚至无需触摸任何AppDelegate代码.我更喜欢b/c可以节省时间和精力的简单方法.因此,请在将要出现按钮动画的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. 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:

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

}

这篇关于UIView动画和UIButton停止按b/c正常工作-迅速4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆