首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIView无限360度旋转动画?

UIView无限360度旋转动画?
EN

Stack Overflow用户
提问于 2012-03-24 02:53:04
回答 28查看 168.2K关注 0票数 259

我正在尝试将UIImageView旋转360度,我已经在网上看了几个教程。我不能让它们中的任何一个工作,除非UIView停止或跳到一个新的位置。

  • 如何实现这一点?

我尝试过的最新方法是:

代码语言:javascript
复制
[UIView animateWithDuration:1.0
                      delay:0.0
                    options:0
                 animations:^{
                     imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];

但是如果我使用2*pi,它根本不会移动(因为它是相同的位置)。如果我尝试只做pi (180度),它可以工作,但如果我再次调用该方法,它会向后旋转。

编辑

代码语言:javascript
复制
[UIView animateWithDuration:1.0
                      delay:0.0
                    options:0
                 animations:^{
                     [UIView setAnimationRepeatCount:HUGE_VALF];
                     [UIView setAnimationBeginsFromCurrentState:YES];
                     imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];

也不管用。它转到180度,暂停片刻,然后重新设置为0度,然后再次启动。

EN

回答 28

Stack Overflow用户

回答已采纳

发布于 2012-03-24 03:41:17

我找到了一个非常适合我的方法(我对它做了一点修改):iphone UIImageView rotation

代码语言:javascript
复制
#import <QuartzCore/QuartzCore.h>

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat {
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat ? HUGE_VALF : 0;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
票数 336
EN

Stack Overflow用户

发布于 2012-06-28 18:18:12

理查德·J·罗斯三世的想法值得称赞,但我发现他的代码并不完全符合我的需要。我相信,options的默认设置是提供UIViewAnimationOptionCurveEaseInOut,这在连续的动画中看起来不太对劲。此外,我添加了一个检查,以便在需要时可以在偶数四分之一圈停止我的动画(不是infinite,而是infinite duration),并使加速度在前90度期间上升,在最后90度期间减速(在请求停止后):

代码语言:javascript
复制
// an ivar for your class:
BOOL animating;

- (void)spinWithOptions:(UIViewAnimationOptions)options {
   // this spin completes 360 degrees every 2 seconds
   [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) {
                             // if flag still set, keep spinning with constant speed
                             [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                          } else if (options != UIViewAnimationOptionCurveEaseOut) {
                             // one last spin, with deceleration
                             [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)的能力,而前一个旋转正在逐渐结束(完成)。示例项目here on Github

票数 112
EN

Stack Overflow用户

发布于 2015-02-26 01:09:56

在Swift中,您可以使用以下代码进行无限旋转:

Swift 4

代码语言:javascript
复制
extension UIView {
    private static let kRotationAnimationKey = "rotationanimationkey"

    func rotate(duration: Double = 1) {
        if layer.animation(forKey: UIView.kRotationAnimationKey) == nil {
            let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

            rotationAnimation.fromValue = 0.0
            rotationAnimation.toValue = Float.pi * 2.0
            rotationAnimation.duration = duration
            rotationAnimation.repeatCount = Float.infinity

            layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey)
        }
    }

    func stopRotating() {
        if layer.animation(forKey: UIView.kRotationAnimationKey) != nil {
            layer.removeAnimation(forKey: UIView.kRotationAnimationKey)
        }
    }
}

Swift 3

代码语言:javascript
复制
let kRotationAnimationKey = "com.myapplication.rotationanimationkey" // Any key

func rotateView(view: UIView, duration: Double = 1) {
    if view.layer.animationForKey(kRotationAnimationKey) == nil {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = Float(M_PI * 2.0)
        rotationAnimation.duration = duration
        rotationAnimation.repeatCount = Float.infinity

        view.layer.addAnimation(rotationAnimation, forKey: kRotationAnimationKey)
    }
}

停止就像:

代码语言:javascript
复制
func stopRotatingView(view: UIView) {
    if view.layer.animationForKey(kRotationAnimationKey) != nil {
        view.layer.removeAnimationForKey(kRotationAnimationKey)
    }
}
票数 104
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9844925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档