2016-07-29 65 views
0

我目前有一個用作視頻進度指示器的動畫,效果很好。不過,現在我們有一項功能,允許用戶通過點擊它跳過視頻前2.5秒,因此我正在嘗試實施進度指示器動畫以向動畫中跳過2.5秒。我如何將動畫向前跳過2.5秒?我試過「animationGroup.timeOffset = 2.5」,但它不起作用。在CABasicAnimation(iOS)中跳過

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

} 

//這裏是我的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) 
} 

回答

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

} 
1

啓動它這篇如果您使用timeOffset跳過一部分動畫,它仍將在相同的總時間內播放。動畫只是循環播放,並再次播放,直到它最初開始的地步。 例如:動畫:A-> B-> C。如果你使用timeOffSet爲了從B開始,它將是:B-> C-> A

在這種情況下,我認爲你可能會放棄這個動畫,然後添加一個新的動畫。

+0

是的,這就是我不得不做的(見我的答案) –