【问题标题】:Resume animateWithDuration .Repeat after return to foreground恢复 animateWithDuration 。返回前台后重复
【发布时间】:2016-08-25 09:47:05
【问题描述】:

不幸的是,我发现UIView.animateWithDuration 在您将应用程序最小化到主屏幕后停止,并且在您将其带回前台后不会恢复。

我花了最后一个小时试图弄清楚如何解决这个问题(通过检测背景/前景切换)并最终添加了一个观察者。我做到了,并在控制台中通过调试消息成功检测到它;但是,我确定如何在我的视图中恢复动画。当应用重新加载到前台时,暂停/恢复甚至重新启动动画的正确方法是什么?

ViewController.swift

class ViewController: UIViewController {
    func cameBackFromSleep(sender : AnyObject) {
        // Restart animation here or resume?
        print ("If you can see this, congrats... observer works :-)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Observer to detect return from background
        NSNotificationCenter.defaultCenter().addObserver(    self,    selector: #selector(ViewController0.cameBackFromSleep(_:)),    name: UIApplicationDidBecomeActiveNotification,    object: nil    )

        // Add a label
        let label = UILabel(frame: CGRectMake(0, 600 , 200, 200 ))
        label.text = "test message"
        label.font = UIFont.boldSystemFontOfSize(12)
        label.sizeToFit()
        self.view.addSubview(label)

        // Animation to move label
        func animateText() {
            UIView.animateWithDuration(2.0, delay: 0.0, options: [ .Autoreverse, .Repeat, .CurveEaseInOut, .BeginFromCurrentState], animations: {
                label.alpha = 0.3
                label.frame.origin.x = ((global.maxwidth/2) * -1) 
                }, completion: { finished in
                    if finished {
                        label.frame.origin.x = 0.0
                    }
            })
        }

        // This guy here stops once you minimize the app to background
        animateText()
    }
}

【问题讨论】:

    标签: swift uiview background foreground animatewithduration


    【解决方案1】:

    animateText() 放入comeBackFromSleep(_:)。我认为恢复动画有点困难,所以只需重新启动它。

    因此,这种行为在某种程度上是有道理的(您可以自己查看AppDelegate 的方法):

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    

    它指出您应该刷新 UI(包括动画)。因此,将应用置于后台后,动画停止是正常行为。

    【讨论】:

    • 我是 Swift/iOS 开发的新手。既然comeBackFromSleep() 超出了viewDidLoad() 的范围,我怎么称呼animateText()viewDidLoad() 内?
    • 你需要把func animateText() OUTSIDE viewDidLoad()。这样您就可以从任何其他方法访问它。所以把func animateText()放在viewDidLoad()下面,然后在comeBackFromSleep()中输入animateText()
    • 我认为这将是一个简单的解决方案,但我的实际动画函数确实有其他变量,这些变量取决于标签在视图中的宽度,一旦它已经启动。它变得很复杂,我认为我唯一的解决方案是在每次应用程序返回到前台时以某种方式删除标签并使用动画再次将其添加到视图中。
    • 将标签声明为全局变量(就在类开始的下方),如下所示:let label = UILabel()。然后在下面//添加标签删除let,然后就可以了。您可以从类中的任何位置访问标签及其属性。然后照我说的做,应该会奏效。
    • 然后每次将其发送到后台时,您都会删除观察者,然后添加一个新的相同的观察者。这应该可以解决问题。尝试一下,看看会发生什么。键入 NSNotificationCenter.defaultCenter().removeObserver(self) 和在 cameBackFromSleep() 中添加新观察者的行。
    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多