2015-11-04 109 views
0

我創造了一個幸運之輪,我無法阻止它。 我該如何平穩地停止用戶發起的,手勢控制的運動中的圓形對象。我能夠突然停止對象,但希望逐漸平穩地停下來。如何順利暫停動畫?

let spinAnimation = CABasicAnimation() 
    spinAnimation.fromValue = 0 
    // goes to 360 (2 * π) 
    spinAnimation.toValue = M_PI*2 
    // define how long it will take to complete a 360 
    spinAnimation.duration = 10 
    // make it spin infinitely 
    spinAnimation.repeatCount = Float.infinity 
    // do not remove when completed 
    spinAnimation.removedOnCompletion = false 
    // specify the fill mode 
    spinAnimation.fillMode = kCAFillModeForwards 
    // and the animation acceleration 
    spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 

    counterView.layer.backgroundColor = UIColor.redColor().CGColor 

    // add the animation to the button layer 
    counterView.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z") 

回答

1

變化
spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)


spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
隨着時間的推移緩出應該放慢動畫。有關計時的詳細信息,請參見https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CAMediaTimingFunction_class/#//apple_ref/doc/constant_group/Predefined_Timing_Functions

+0

它的任何示例?我試圖讓車輪停下來,但不能回到起點。 – Peter

+1

然後不要做'M_PI * 2'的值,而是'M_PI *基於隨機或強制的某個數字'確保隨機數是一個小數,否則你只會做180 – Knight0fDragon