如何暂停和恢复 UIView.animateWithDuration

Posted

技术标签:

【中文标题】如何暂停和恢复 UIView.animateWithDuration【英文标题】:How to pause and resume UIView.animateWithDuration 【发布时间】:2015-11-30 08:45:09 【问题描述】:

我有一张图片,我用这段代码在 viewDidAppear 中为它制作动画:

UIView.animateWithDuration(10.5, delay:0.0, options: [], animations:
self.myImage.transform = CGAffineTransformMakeTranslation(0.0, 200)
, completion: nil)

我想在点击myPauseButton时暂停动画,如果再次点击它会恢复动画。

【问题讨论】:

【参考方案1】:

2 个暂停和恢复动画的函数,我取自 here 并转换为 Swift。

func pauseLayer(layer: CALayer) 
    let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: 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(), from: nil) - pausedTime
    layer.beginTime = timeSincePause

我有一个按钮来暂停或恢复在viewDidLoad 中启动的动画:

var pause = false
override func viewDidAppear(animated: Bool) 
    super.viewDidAppear(animated)
    UIView.animate(withDuration: 10.5) 
        self.image.transform = CGAffineTransformMakeTranslation(0.0, 200)
    


@IBAction func changeState() 
    let layer = image.layer
    pause = !pause
    if pause 
        pauseLayer(layer)
     else 
        resumeLayer(layer)
    

【讨论】:

简单、优雅、完美,谢谢! 谢谢,这对我帮助很大!但是,当图层暂停并旋转视图时,应用程序会挂起。我怎样才能防止这种情况发生? 我登录只是为了感谢您并投票! UIViewProbertyAnimator 太愚蠢了,谢谢你【参考方案2】:

这是该答案的 Swift 3 版本 + 我将这些功能移到了扩展中

extension CALayer 
    func pause() 
        let pausedTime: CFTimeInterval = self.convertTime(CACurrentMediaTime(), from: nil)
        self.speed = 0.0
        self.timeOffset = pausedTime
    

    func resume() 
        let pausedTime: CFTimeInterval = self.timeOffset
        self.speed = 1.0
        self.timeOffset = 0.0
        self.beginTime = 0.0
        let timeSincePause: CFTimeInterval = self.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
        self.beginTime = timeSincePause
    

【讨论】:

【参考方案3】:

由于 ios 10 提供了UIViewPropertyAnimator,您可以更轻松地解决您的问题。

在你的控制器中声明这些属性:

var animationPaused = false
lazy var animator: UIViewPropertyAnimator = UIViewPropertyAnimator(duration: 10.5, curve: .easeInOut, animations: 

    self.myImage.transform = CGAffineTransform(translationX: 0.0, y: 200)
)

将以下代码添加到myPauseButton的点击处理程序中:

if self.animator.state == .active  // Don't start or pause the animation when it's finished

    self.animationPaused = !self.animationPaused
    self.animationPaused ? self.animator.pauseAnimation() : self.animator.startAnimation()

使用以下代码行在viewDidAppear(_ animated: Bool) 中启动动画:

self.animationPaused = false
self.animator.startAnimation()

【讨论】:

以上是关于如何暂停和恢复 UIView.animateWithDuration的主要内容,如果未能解决你的问题,请参考以下文章

如何暂停和恢复 UIView 动画(没有块动画)?

如何暂停和恢复 GameLoop?

如何使用暂停和恢复创建 ProgressDialog

如何在 C# 中暂停和恢复音频

如何使用 threading 模块暂停和恢复线程?

Spotify - 如何暂停曲目和恢复曲目