ios - UIView无限360度旋转动画?

我正在尝试将 UIImageView 旋转 360 度,并查看了一些在线教程。如果没有 UIView 停止或跳转到新位置,我无法让它们中的任何一个工作。

  • 我怎样才能做到这一点?

我最近尝试的是:

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

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

编辑:

[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 度。

最佳答案

找到了一种对我来说非常有效的方法(我对其进行了一些修改):iphone UIImageView rotation

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

https://stackoverflow.com/questions/9844925/

相关文章:

objective-c - Xcode 6 中未创建 Swift 到 Objective-C hea

iphone - iOS 7 导航栏文本和箭头颜色

iphone - 如何比较两个 NSDates : Which is more recent?

iOS - 从 ViewController 调用 App Delegate 方法

objective-c - 为设备编译时出现 Apple Mach-O 链接器错误

ios - Objective-C 中的自动引用计数不能防止或最小化什么样的泄漏?

ios - 单个 UILabel 中的粗体和非粗体文本?

ios - 如何在 iOS 上找到最顶层的 View Controller

ios - Pod 安装停留在 "Setting up CocoaPods Master repo"

iphone - 如何使用使用 Interface Builder 创建的 nib 文件加载 UIV