我正在使用带有完成块的 UIViewAnimation
为视图设置动画。当应用程序进入后台模式时,需要暂停动画并从它在前台模式中暂停的位置恢复它。我正在使用波纹管方法进行暂停和恢复:-
func resume() {
let pausedTime: CFTimeInterval = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}
func pause() {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
如果应用程序在前台,这两种方法都可以正常工作。但是如果应用程序进入后台,动画完成并首先调用完成块,然后调用暂停方法,但不会像动画已经完成那样工作。我使用了 UIApplication.willEnterForegroundNotification
和 UIApplication.didEnterBackgroundNotification
根据 this answer 使用 Core Animation
而不是 UIViewAnimation
然后恢复/暂停将起作用。但我没有办法使用 CABasicAnimation
而不是 UIView
动画。
UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: {[weak self] in
if let _self = self {
_self.frame.size.width = width
}
}) { [weak self] (finished) in
// do something after finished animation
}
如果使用 Core Animation
我该怎么做?否则有没有UIView
动画背景暂停和前景恢复的解决方案。
答案 0 :(得分:0)
有关相当详细的讨论,请参阅此主题:
How to pause and resume UIView.animateWithDuration
到目前为止,暂停和恢复动画的最简单、最干净的方法是使用 <a href="{{url('admin/$getStudyLevel->slug_name/$getStudyLevel->id')}}" class="small-box-
footer">Get In <i class="fas fa-arrow-circle-right"></i></a>
,如其中一个答案所述。
当您收到应用暂停通知时,您应该能够捕获动画的完成百分比,然后编写代码以在应用恢复时重新建立动画,并将其设置为动画。