问:暂停与恢复的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