如何重置 Swift 中的循环进度条?
How do I reset a circular progress bar in Swift?
我在Swift中创建了一个圆形进度条。我可以开始、暂停和恢复动画,但我需要能够通过点击按钮重置动画。
这是我用来启动、暂停和恢复动画的代码。重置它还需要哪些额外步骤?
func startAnimation() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = seconds
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "basicAnime")
}
func pauseLayer(layer: CALayer){
let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeAnimation(layer: CALayer){
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}
func resetAnimation() {
timer.invalidate()
// next steps
}
一种可能的解决方案是简单地删除之前添加的动画
有关详细信息,请参阅 CALayer#removeAllAnimations()
and CALayer#removeAnimation(forKey:)
我在Swift中创建了一个圆形进度条。我可以开始、暂停和恢复动画,但我需要能够通过点击按钮重置动画。
这是我用来启动、暂停和恢复动画的代码。重置它还需要哪些额外步骤?
func startAnimation() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = seconds
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "basicAnime")
}
func pauseLayer(layer: CALayer){
let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeAnimation(layer: CALayer){
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}
func resetAnimation() {
timer.invalidate()
// next steps
}
一种可能的解决方案是简单地删除之前添加的动画
有关详细信息,请参阅 CALayer#removeAllAnimations()
and CALayer#removeAnimation(forKey:)