ios – 如何暂停和恢复UIView.animateWithDuration

前端之家收集整理的这篇文章主要介绍了ios – 如何暂停和恢复UIView.animateWithDuration前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个图像,我使用此代码为viewDidAppear设置动画:
UIView.animateWithDuration(10.5,delay:0.0,options: [],animations:{
self.myImage.transform = CGAffineTransformMakeTranslation(0.0,200)
},completion: nil)

我想点击myPauseButton时暂停动画,如果我再次点击它,则恢复动画.

解决方法

暂停和恢复动画的2个功能,我从 here开始转换为Swift.
func pauseLayer(layer: CALayer) {
    let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(),fromLayer: 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(),fromLayer: nil) - pausedTime
    layer.beginTime = timeSincePause
}

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

var pause = false
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    UIView.animateWithDuration(10.5,animations:{

        self.image.transform = CGAffineTransformMakeTranslation(0.0,200)
        },completion: nil)
}

@IBAction func changeState() {
    let layer = image.layer
    pause = !pause
    if pause {
        pauseLayer(layer)
    } else {
        resumeLayer(layer)
    }
}
原文链接:https://www.f2er.com/iOS/335115.html

猜你在找的iOS相关文章