2017-08-26 31 views
3

我使用此代码来旋转任何观点:平稳加快的运行旋转动画

func rotateAnimation(theView: UIView, duration: CFTimeInterval = 20.0, repeatCount: Float = 200, toValue: CGFloat = CGFloat(.pi * 2.0)) { 
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
    rotateAnimation.fromValue = 0.0 
    rotateAnimation.toValue = toValue 
    rotateAnimation.duration = duration 
    rotateAnimation.repeatCount = repeatCount 
    theView.layer.add(rotateAnimation, forKey: nil) 
} 

我想知道如何加快速度,目前正在运行的动画。例如,如果一个UIView随上面的代码一起旋转,那么如何使这个速度以一个很好的转换速度快两倍?

因此:调用rotationAnimation:以正常速度旋转 - >调用?函数? - > UIView将平稳地旋转到更快的速度 - > UIView将以所需的速度继续旋转。

感谢您的任何帮助。

+0

没有改变持续时间的帮助? – 3stud1ant3

+0

@ 3stud1ant3不会,它会改变速度,但几秒钟后动画会恢复到原来的速度。 – NoKey

+0

在改变持续时间的同一个音符上,你是否尝试过一段时间循环,条件行指定了动画的最终位置? – ProgrammingEnthusiast

回答

2

请尝试以下代码

 let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in 
      self.viewToMove.layer.timeOffset = self.viewToMove.layer.convertTime(CACurrentMediaTime(), from: nil) 
      self.viewToMove.layer.beginTime = CACurrentMediaTime() 
      self.viewToMove.layer.speed += 0.5 

     } 
     timer.fire() 

它会增加动画速度非常流畅

+0

是的,谢谢你它与几行代码:) –

0

试试这个第二动画

您可以设置较小的时间来提高速度

func rotateAnimation(theView: UIView, duration: CFTimeInterval = 0.4, repeatCount: Float = 200, toValue: CGFloat = CGFloat(.pi * 2.0)) { 
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
    rotateAnimation.fromValue = 0.0 
    rotateAnimation.toValue = toValue 
    rotateAnimation.duration = duration 
    rotateAnimation.repeatCount = repeatCount 

并设置shouldRasterize =真有更好的表现

theView.layer.rasterizationScale = UIScreen.main.scale 
    theView.layer.shouldRasterize = true 

使用两个相同的密钥动画

theView.layer.add(rotateAnimation, forKey: "animation") 
} 
0

将repeatcount设置为1,并在每个动画停止后重新添加动画。我不确定这是否会导致性能问题,因为我使用非常基本的方形视图进行测试。

通过添加CAMediaTimingFunction参数,你可以控制是否使用kCAMediaTimingFunctionLinear - 为那些正常的动画循环,或者使用一个自定义,如kCAMediaTimingFunctionEaseIn - 为关键帧动画显示的平滑过渡。

// your function to start the acceleration 
@IBAction func buttonTapped(_ sender: UIButton) { 
    accel = true 
    speed = 2.0 
    timeOffset = self.rect.layer.convertTime(CACurrentMediaTime(), from: nil) 
    timeOffset = timeOffset - lastStart 
    // memorize the timeoffset, new speed (2x) and a boolean flag 
    self.rect.layer.removeAllAnimations() 
} 

// delegate - memorize the CFTimeInterval 
func animationDidStart(_ anim: CAAnimation) { 
    lastStart = self.rect.layer.convertTime(CACurrentMediaTime(), from: nil) 
} 

// delegate - start a new loop of animation each time it's stopped 
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { 
    if accel { 
     accel = false 
     let opt = CAMediaTimingFunction(controlPoints: 0.5, 0.2, 0.9, 0.7) 
     rotateAnimation(theView: rect, speed: speed, fromValue: CGFloat(.pi * 2.0 * Float(timeOffset)/10.0), toValue: CGFloat(.pi * 2.0 * Float(timeOffset)/10.0 + .pi * 2.0), option: opt) 
    } else { 
     rotateAnimation(theView: rect, speed: speed, fromValue: CGFloat(.pi * 2.0 * Float(timeOffset)/10.0), toValue: CGFloat(.pi * 2.0 * Float(timeOffset)/10.0 + .pi * 2.0)) 
     // note that timeOffset is initialize to be 0. 
    } 
} 

你想在这里自定义CAMediaTimingFunction,通过0.5的斜率开始,逐渐增加至1.0,我这里没有优化的贝塞尔曲线,你可以做自己想要的曲线。

最后一个小调整到您的动画功能:

func rotateAnimation(theView: UIView, speed: Float, duration: CFTimeInterval = 10.0, fromValue: CGFloat = CGFloat(0.0), toValue: CGFloat = CGFloat(.pi * 2.0), option: CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)) { 
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
    rotateAnimation.fromValue = fromValue 
    rotateAnimation.toValue = toValue 
    rotateAnimation.duration = duration 
    rotateAnimation.speed = speed 
    rotateAnimation.repeatCount = 1 
    rotateAnimation.delegate = self 
    rotateAnimation.timingFunction = option 
    theView.layer.add(rotateAnimation, forKey: nil) 
}