UIView无限360度旋转动画? [英] UIView Infinite 360 degree rotation animation?
问题描述
我正在尝试旋转 UIImageView
360度,并在线查看了几个教程。如果没有 UIView
停止或跳到新位置,我就无法使用它们。
I'm trying to rotate a UIImageView
360 degrees, and have looked at several tutorials online. I could get none of them working, without the UIView
either stopping, or jumping to a new position.
- 我怎样才能实现这个目标?
我尝试过的最新事情是:
The latest thing I've tried is:
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
但是如果我使用2 * pi,它根本不会移动(因为它是相同的位置)。如果我尝试做pi(180度),它可以工作,但是如果我再次调用该方法,它会向后旋转。
But if I use 2*pi, it doesn't move at all (since it's the same position). If I try to do just pi (180 degrees), it works, but if I call the method again, it rotates backwards.
编辑 :
[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
度。
doesn't work either. It goes to 180
degrees, pauses for a split second, then resets back to 0
degrees before it starts again.
推荐答案
找到一个方法(我修改了一下),对我来说非常合适: iphone UIImageView旋转
Found a method (I modified it a bit) that worked perfectly for me: 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"];
}
这篇关于UIView无限360度旋转动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!