gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:32:43 27 4
gpt4 key购买 nike

我有一张图片,我在 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(), 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)
}
}

关于ios - 如何暂停和恢复 UIView.animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com