如何暂停和恢复UIView.animateWithDuration

我在viewDidAppear中有一个图像,并用以下代码对其进行了动画处理:

UIView.animateWithDuration(10.5, delay:0.0, options: [], animations:{

self.myImage.transform = CGAffineTransformMakeTranslation(0.0, 200)

}, completion: nil)

我想在点击时暂停动画myPauseButton,如果再次点击它则继续播放动画。

回答:

暂停和恢复动画的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

}

我有一个按钮可以暂停或恢复在中初始化的动画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)

}

}

以上是 如何暂停和恢复UIView.animateWithDuration 的全部内容, 来源链接: utcz.com/qa/421542.html

回到顶部