gpt4 book ai didi

ios - 当应用程序进入后台时暂停和恢复 UIView 动画

转载 作者:可可西里 更新时间:2023-10-31 23:44:48 30 4
gpt4 key购买 nike

我正在为一个 View 设置动画,我想暂停它并恢复它。

使用苹果指南我创建了一个 CALayer 扩展

extension CALayer {

func pause() {
var pauseTime = self.convertTime(CACurrentMediaTime(), fromLayer: nil)
self.speed = 0.0
self.timeOffset = pauseTime
}

func resume() {
var pausedTime = self.timeOffset
self.speed = 1.0
self.timeOffset = 0.0
self.beginTime = 0.0
var timeSincePause = self.convertTime(CACurrentMediaTime(), toLayer: nil) - pausedTime

self.beginTime = timeSincePause
}
}

除非该应用程序进入后台,否则此代码工作正常。当我将应用程序带回前台时,动画已完成(即使时间未过去),并且当我单击恢复时它不会再次启动。

好的。我尝试为 CALayer 设置动画,但我遇到了同样的问题。

extension CALayer {

func animateY(newY:CGFloat,time:NSTimeInterval,completion:()->Void){
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
let animation = CABasicAnimation(keyPath: "position.y")
animation.fromValue = self.position.y
animation.toValue = newY
animation.duration = time
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.removedOnCompletion = false // don't remove after finishing
self.position.y = newY
self.addAnimation(animation, forKey: "position.y")
CATransaction.flush()

}
}

最佳答案

我推荐使用CABasicAnimation。您的恢复/暂停方法应该没问题,因为它们来自 answer.您应该尝试使用 Core Animation 而不是 UIViewAnimation 然后恢复/暂停将起作用。

然后您可以注册两个通知 UIApplicationWillEnterForegroundNotificationUIApplicationDidEnterBackgroundNotification 以完全控制暂停/恢复操作。

关于ios - 当应用程序进入后台时暂停和恢复 UIView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30597399/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com