简体   繁体   English

在CABasicAnimation(iOS)中向前跳

[英]Skip forward in CABasicAnimation (iOS)

I currently have an animation which is used as a progress indicator for a video, works great. 我目前有一个动画,可以用作视频的进度指示器,效果很好。 However, now we have a feature that allows the user to skip 2.5 seconds forward into the video by tapping it, and thus I am trying to implement the progress indicator animation to skip forward 2.5 seconds into the animation. 但是,现在我们有了一项功能,允许用户通过点按它可以将视频向前跳过2.5秒,因此,我正在尝试实现进度指示器动画,从而将其向前跳过2.5秒。 How would I skip the animation forward 2.5 seconds? 如何跳过动画2.5秒钟? I've tried "animationGroup.timeOffset = 2.5" but it doesn't work. 我已经尝试过“ animationGroup.timeOffset = 2.5”,但是它不起作用。

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")

}

//Here is where my externalCircle is drawn: //在这里绘制我的externalCircle:

private func drawCircles() {

    let externalCirclePath = UIBezierPath(roundedRect: CGRectMake(0, 0, bounds.height, bounds.height), cornerRadius: bounds.height / 2)
    externalCircle.path = externalCirclePath.CGPath
    externalCircle.fillColor = UIColor.clearColor().CGColor
    externalCircle.strokeColor = UIColor.whiteColor().CGColor
    externalCircle.lineWidth = 2

    let internalCircleRadius = bounds.size.height / 5
    let internalCirclePath = UIBezierPath(roundedRect: CGRectMake(0, 0, internalCircleRadius * 2, internalCircleRadius * 2), cornerRadius: internalCircleRadius)
    internalCircle.path = internalCirclePath.CGPath
    internalCircle.fillColor = UIColor.whiteColor().CGColor
    internalCircle.position = CGPointMake(CGRectGetMidX(bounds) - internalCircleRadius,
                                          CGRectGetMidY(bounds) - internalCircleRadius);

    layer.addSublayer(internalCircle)
    layer.addSublayer(externalCircle)
}

If you skip a proportion of animation using the timeOffset, it will still play for the same total duration. 如果您使用timeOffset跳过一定比例的动画,则动画仍将播放相同的总持续时间。 The animation simply loops around and plays again up until the point where it originally started. 动画只是循环播放并再次播放,直到它最初开始为止。 Ex: animation : A->B->C. 例如:动画:A-> B-> C。 If you use timeOffSet in order to begin at B, it will be : B->C->A 如果您使用timeOffSet为了从B开始,它将是:B-> C-> A

In this circumstance, I think you may dismiss this animation, then add new one. 在这种情况下,我认为您可以关闭此动画,然后添加新的动画。

Did this by removing the previous animation and starting it based on the percentage elapsed (with skip) 通过删除之前的动画并根据经过的百分比(使用跳过)启动动画来完成此操作

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")

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM