恢复 animateWithDuration 。在 return 之后重复到前景
Resume animateWithDuration .Repeat after return to foreground
不幸的是,我发现 UIView.animateWithDuration
一旦您将您的应用程序最小化到主屏幕就会停止,并且一旦您将它带回前台就不会恢复。
我花了最后一个小时试图弄清楚如何解决这个问题(通过检测 background/foreground 切换)并导致添加一个观察者。我做到了,并在控制台中通过调试消息成功检测到它;但是,我确定如何在我的视图中恢复动画。当应用程序重新加载到前台时,pause/resume 甚至重新启动动画的正确方法是什么?
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()
}
}
把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(包括动画)。因此,将应用置于后台后动画停止是正常现象。
不幸的是,我发现 UIView.animateWithDuration
一旦您将您的应用程序最小化到主屏幕就会停止,并且一旦您将它带回前台就不会恢复。
我花了最后一个小时试图弄清楚如何解决这个问题(通过检测 background/foreground 切换)并导致添加一个观察者。我做到了,并在控制台中通过调试消息成功检测到它;但是,我确定如何在我的视图中恢复动画。当应用程序重新加载到前台时,pause/resume 甚至重新启动动画的正确方法是什么?
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()
}
}
把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(包括动画)。因此,将应用置于后台后动画停止是正常现象。