关于Objective C:如何使用iPhone SDK无限旋转360度图像?

How to rotate image 360 degrees endlessly using iPhone SDK?

我想使用iPhone SDK将图像无限旋转360度吗?

这怎么办?

谢谢!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    spinWheel.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
    CGAffineTransform transform = spinWheel.transform;
    float fromAngle = atan2(transform.b, transform.a);
    float toAngle = fromAngle + (totalRevolutions*4*M_PI);
    spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
    spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
    spinAnimation.repeatCount = 0;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseOut];
    [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    [CATransaction commit];
}

尝试使用它,它起作用。


假设您要在UIImageView中显示图像,则可以旋转视图的局部坐标系(请参见此处),并使用NSTimer定期增加旋转(请参见此处)。

(我将为此使用OpenGL,但这只是个人喜好,可能对您的情况有些大意。)