xcode 如何使用uiview动画完全旋转圆形对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12361096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:27:01  来源:igfitidea点击:

How to have full rotation of a circular object using uiview animation

iosobjective-cswiftxcodeuiviewanimation

提问by user1620383

I am trying to loop over partial circles to achieve the animation but it only turns 180 degrees, and then starts again from 0 degrees to 180 degrees. So there is an abrupt jump from 180 degrees to 360 degrees. How can I have my circular image object rotate continuously without any jumps? Here is my current code:

我试图循环部分圆圈以实现动画,但它只旋转 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;
                }];

回答by Hiren

Spin UI Object, animation rotate 360 Degree
you'll need to add the QuartzCore Framework to your project and include the

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

#import QuartzCore/QuartzCore.h

Add animation for object:

为对象添加动画:

 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"];

回答by ggfela

I found some old code that I use to rotate a view 360 degrees, I hope it helps. You'll need to import QuartzCore.

我找到了一些用于 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"];

回答by Krunal

There are following different ways to perform 360 degree animation with UIView.

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

Using CABasicAnimation

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


Here is an extension functions for UIView that handles start & stop rotation operations:


这是处理开始和停止旋转操作的 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")
    }
}


Now using, UIView.animationclosure:


现在使用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))
    }
}

回答by jay sharma

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; 
}

You can start and stop the view animation by calling startSpin and stopSpin methods respectively.

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

And here is the swift relevant answer:

这是快速相关的答案:

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
}