4

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

4 回答 4

14

旋转 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"];
于 2012-09-11T06:56:28.163 回答
3

我发现了一些用于将视图旋转 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"];
于 2012-09-11T06:50:49.820 回答
1

使用 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))
    }
}
于 2017-05-02T13:16:10.123 回答
-1
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
}
于 2018-04-03T11:50:38.667 回答