简体   繁体   English

如何暂停和恢复 UIView.animateWithDuration

[英]How to pause and resume UIView.animateWithDuration

I have an image, I animate it with this code, in viewDidAppear:我有一个图像,我在 viewDidAppear 中使用此代码对其进行动画处理:

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

I want to pause the animation when I tap myPauseButton , and resume the animation if I tap it again.我想在点击myPauseButton时暂停动画,如果再次点击它,则恢复动画。

2 functions to pause and resume animation, I take from here and convert to Swift. 2 个暂停和恢复动画的函数,我从 这里获取并转换为 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
}

I have a button to pause or resume the animation which is initiated in viewDidLoad :我有一个按钮可以暂停或恢复在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)
    }
}

Here is Swift 3 version of that answer + I moved those function to an extension这是该答案的 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
    }
}

Since iOS 10 provides UIViewPropertyAnimator you can solve your problem easier.由于 iOS 10 提供了UIViewPropertyAnimator您可以更轻松地解决问题。

Declare these properties in your controller:在控制器中声明这些属性:

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

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

Add the following code to the tap handler of myPauseButton :将以下代码添加到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()
}

Start the animation in viewDidAppear(_ animated: Bool) with these lines of code:使用以下代码行在viewDidAppear(_ animated: Bool)启动动画:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM