問:暫停與恢復的UIView動畫與Alpha當用戶按下Home鍵不起作用
- 如何暫停嵌套的UIView動畫以動畫的α/阻當用戶按下主頁按鈕並返回到應用程序?
- 我在下面的代碼中錯過了什麼?謝謝。
背景:
我有一個簡單的嵌套的UIView動畫在不同的時間緩慢地調整的圖的α從alpha = 0
到alpha = 1
超過1分鐘。
當用戶按下主頁按鈕時,UIView動畫不會暫停,因此當應用程序恢復時,UIView動畫快進到動畫的最後,alpha在恢復應用程序時立即從0變爲1 。
所以,我試圖暫停UIView動畫,當用戶按下設備的主頁按鈕並暫時掛起應用程序。
我有一個函數暫停動畫(pauseLayer
),然後重新開始動畫(resumeLayer
)。從UIButton
撥打電話時,這兩個功能非常有用。它暫停alpha
並按預期恢復動畫。 (然而,如果被按壓主頁按鈕,而動畫暫停時,當它恢復所述α的變化瞬間從0到1)
當我嘗試調用pauseLayer
當用戶按壓主頁按鈕(接收WillResignActive
通知),然後返回到應用程序(接收到WillEnterForeground
通知),則動畫不會暫停並繼續,而是在恢復應用程序時alpha會立即從0更改爲1。
它似乎應該工作,但事實並非如此。
代碼:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView1: UIView!
@IBOutlet weak var myView2: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setNotifications()
myAnimation()
}
@IBAction func myPauseButton(sender: UIButton) {
let layer = self.view.layer
pauseLayer(layer)
}
@IBAction func myResumeButton(sender: UIButton) {
let layer = self.view.layer
resumeLayer(layer)
}
func setNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.WillEnterForeground(_:)), name: UIApplicationWillEnterForegroundNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.WillResignActive(_:)), name: UIApplicationWillResignActiveNotification, object: nil)
}
func WillEnterForeground(notification : NSNotification) {
let layer = self.view.layer
resumeLayer(layer)
}
func WillResignActive(notification : NSNotification) {
let layer = self.view.layer
pauseLayer(layer)
}
func pauseLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
layer.beginTime = timeSincePause
}
func myAnimation() {
self.myView1.alpha = 0
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView1.alpha = 0.1
}, completion: {(finished: Bool) in
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView1.alpha = 0.2
}, completion: {(finished: Bool) in
UIView.animateWithDuration(30, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView1.alpha = 1
}, completion: nil)
})
})
self.myView2.alpha = 0
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView2.alpha = 0.1
}, completion: {(finished: Bool) in
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView2.alpha = 0.2
}, completion: {(finished: Bool) in
UIView.animateWithDuration(30, delay: 0, options: [.CurveEaseInOut], animations: {
self.myView2.alpha = 1
}, completion: nil)
})
})
}
}
編輯:
如果我添加if (finished) {
到每個部分,然後按Home鍵,然後返回到應用程序中,動畫只進行到下一個部分並停止,不再進一步。這樣比較好,但是問題在於resumeLayer
似乎不起作用,所以動畫保持停止狀態,不會繼續。
self.myView2.alpha = 0
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut, .LayoutSubviews, .AllowUserInteraction, .BeginFromCurrentState], animations: {
self.myView2.alpha = 0.1
}, completion: {(finished: Bool) in
if (finished) {
UIView.animateWithDuration(15, delay: 0, options: [.CurveEaseInOut, .LayoutSubviews, .AllowUserInteraction, .BeginFromCurrentState], animations: {
self.myView2.alpha = 0.2
}, completion: {(finished: Bool) in
if (finished) {
UIView.animateWithDuration(30, delay: 0, options: [.CurveEaseInOut, .LayoutSubviews, .AllowUserInteraction, .BeginFromCurrentState], animations: {
self.myView2.alpha = 1
}, completion: nil)
}
})
}
})
也許不是UIApplicationWillEnterForeground的,你應該使用UIApplicationDidBecomeActive – DerrickHo328
不幸的是,沒有工作@ Calimari328但感謝,雖然,我沒有嘗試。 – user4806509