【问题标题】:How to have full rotation of a circular object using uiview animation如何使用 uiview 动画完全旋转圆形对象
【发布时间】:2012-09-11 00:13:08
【问题描述】:

我正在尝试循环部分圆圈以实现动画,但它只转动 180 度,然后从 0 度重新开始到 180 度。所以有一个从 180 度到 360 度的突然跳跃。如何让我的圆形图像对象连续旋转而没有任何跳跃?这是我当前的代码:

    UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat;

    [UIView animateWithDuration:ROTATE_ANIMATION_DURATION/2 delay:0 options:options 
       animations:^{
           view.transform = CGAffineTransformRotate(transform, M_PI);}//1st step of animation finished
       completion:^(BOOL finished) {
             [UIView animateWithDuration:ROTATE_ANIMATION_DURATION/2 delay:0 options:options 
                animations:^{
                    view.transform = CGAffineTransformRotate(transform, M_PI);} //2nd step of animation finished
                completion:^(BOOL finished) {nil;
                }];

【问题讨论】:

  • 现在用CA Animation做这个,还是想看看有没有UIView动画块的解决方案。

标签: ios objective-c swift xcode uiviewanimation


【解决方案1】:

旋转 UI 对象,动画旋转 360 度
您需要将 QuartzCore 框架添加到您的项目中并包含

#import QuartzCore/QuartzCore.h

为对象添加动画:

 CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 6;
    fullRotation.repeatCount = 1e100f;
    [myview.layer addAnimation:fullRotation forKey:@"360"];

【讨论】:

  • 这似乎是 2013 年最好的方式——即使现在块在这里!
【解决方案2】:

我发现了一些用于将视图旋转 360 度的旧代码,希望对您有所帮助。您需要导入 QuartzCore。

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.zRad"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];

rotationAnimation.duration = 3;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = INFINITY;
rotationAnimation.timingFunction = 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

[viewToAnimate.layer addAnimation:rotationAnimation forKey:@"transform.rotation.zRad"];

【讨论】:

  • 谢谢,我现在正在使用 CA Animation。奇怪的是,我们无法使用 UIView Animation 完成这个简单的动画。
  • 是的,我也尝试了 UIView 动画块,但没有成功。
【解决方案3】:

使用 UIView 执行 360 度动画有以下不同的方式。

使用 CABasicAnimation

var rotationAnimation = CABasicAnimation()
rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: (Double.pi * 2.0))
rotationAnimation.duration = 1.0
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = 100.0
view.layer.add(rotationAnimation, forKey: "rotationAnimation")


这是处理开始和停止旋转操作的 UIView 的扩展函数:

extension UIView {

    // Start rotation
    func startRotation() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.fromValue = 0
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1.0
        rotation.isCumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.add(rotation, forKey: "rotationAnimation")
    }

    // Stop rotation
    func stopRotation() {
        self.layer.removeAnimation(forKey: "rotationAnimation")
    }
}


现在使用 UIView.animation 闭包:

UIView.animate(withDuration: 0.5, animations: { 
      view.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)) 
}) { (isAnimationComplete) in
    UIView.animate(withDuration: 0.5) { 
      view.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))
    }
}

【讨论】:

    【解决方案4】:
    BOOL animating;
    
    - (void)spinWithOptions:(UIViewAnimationOptions)options {  
    [UIView animateWithDuration:0.5
                                 delay:0
                               options:options
                            animations:^{
    
                               self.imageToMove.transform = CGAffineTransformRotate(imageToMove.transform, M_PI / 2);
                            }
                            completion:^(BOOL finished) {
                               if (finished) {
                                  if (animating) {
                                     [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                                  } else if (options != UIViewAnimationOptionCurveEaseOut) {
                                     [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                                  }
                               }
                            }];
    }
    - (void)startSpin {   
         if (!animating) {
            animating = YES;
            [self spinWithOptions: UIViewAnimationOptionCurveEaseIn];    
         } 
    }
    
    - (void)stopSpin  {
        // set the flag to stop spinning after one last 90 degree increment
        animating = NO; 
    }
    

    您可以通过分别调用 startSpin 和 stopSpin 方法来启动和停止视图动画。

    这里是快速的相关答案:

    func spin(with options: UIViewAnimationOptions) {
                 UIView.animate(withDuration: 0.5, delay: 0, options: options,
            animations: {() -> Void in
    
                        self.imageToRotate.transform = self.imageToRotate.transform.rotated(by: .pi / 2)  
                    }, completion: {(_ finished: Bool) -> Void in
    
                if finished {
                   if self.animating {
                      self.spin(with: .curveLinear)
                   }
                   else if options != .curveEaseOut {
                      self.spin(with: .curveEaseOut)
                   }
                }
            })
    }
    
    func startSpin() {
       if !animating {
          animating = true
          spin(with: .curveEaseIn)
       }
    }
    
    func stopSpin() {
        animating = false
    }
    

    【讨论】:

    • 请详细说明为什么这段代码会这样做,现在它只是一堵代码
    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多