【问题标题】:How to pause and resume UIView.animateWithDuration如何暂停和恢复 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时暂停动画,如果再次点击它会恢复动画。

【问题讨论】:

    标签: ios swift uiview


    【解决方案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()
      

      【讨论】:

        猜你喜欢
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-04
        • 1970-01-01
        • 2013-07-19
        • 2015-07-28
        相关资源
        最近更新 更多