我目前有一个动画用作视频的进度指示器,效果很好。但是,现在我们有一个功能,允许用户通过点击它跳过2.5秒前进到视频,因此我试图实现进度指示器动画向前跳过动画2.5秒。我如何向前跳过动画2.5秒?我试过" animationGroup.timeOffset = 2.5"但它没有用。
for x in array:
if(condition):
do stuff
else:
do other stuff
//这是我绘制externalCircle的地方:
func performProgressIndicatorAnimation(duration: Float64) {
layer.mask = nil
layer.speed = 1.0
self.duration = duration
let strokeStartAnimation = CABasicAnimation(keyPath: "strokeStart")
strokeStartAnimation.fromValue = 0
strokeStartAnimation.toValue = 1
strokeStartAnimation.duration = duration
strokeStartAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animationGroup = CAAnimationGroup()
animationGroup.duration = duration
animationGroup.animations = [strokeStartAnimation]
externalCircle.addAnimation(animationGroup, forKey: "animationGroup")
}
答案 0 :(得分:1)
如果使用timeOffset跳过一定比例的动画,它仍会播放相同的总持续时间。动画只是循环播放并再次播放,直到它最初开始的位置。 例如:动画:A-> B-> C。如果你使用timeOffSet从B开始,它将是:B-> C-> A
在这种情况下,我认为您可以忽略此动画,然后添加新动画。
答案 1 :(得分:0)
通过删除上一个动画并根据经过的百分比(跳过)启动它
func skipProgressIndicatorAnimation(currentTime: CMTime, timeToSkip: Double, duration: CMTime) {
animationGroup = nil
externalCircle.removeAllAnimations()
let percentageElapsedWithSkip = (CMTimeGetSeconds(currentTime) + timeToSkip) / CMTimeGetSeconds(duration)
let strokeStartAnimation = CABasicAnimation(keyPath: "strokeStart")
strokeStartAnimation.fromValue = percentageElapsedWithSkip
strokeStartAnimation.toValue = 1
strokeStartAnimation.duration = CMTimeGetSeconds(duration) - CMTimeGetSeconds(currentTime) - timeToSkip
strokeStartAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animationGroup = CAAnimationGroup()
animationGroup.duration = CMTimeGetSeconds(duration) - CMTimeGetSeconds(currentTime) - timeToSkip
animationGroup.animations = [strokeStartAnimation]
externalCircle.addAnimation(animationGroup, forKey: "animationGroup")
}