2010-08-07 84 views
5

How do you旋轉UIViewUIImageViewCALayer動畫360度不使用OpenGL ES?如何旋轉UIView,UIImageView或CALayer動畫360˚?

+3

你知道那360˚旋轉會給你完全相同的圖像,對不對? – SteamTrout 2010-08-07 19:37:33

+0

我感到很難過,在我意識到這一點之前,我開始試圖想出一個解決方案。 – jtbandes 2010-08-07 19:42:53

+0

@Steam Trout:只有當你想要它是瞬間的。 – JeremyP 2010-08-07 20:19:36

回答

3

大概你的意思是動畫旋轉一個UIImage圍繞一個完整的360°?根據我的經驗,完成一個完整的圓旋轉的方式是鏈多個動畫一起:

- (IBAction)rotate:(id)sender 
{ 
    [UIView beginAnimations:@"step1" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
    [imageView.transform = CGAffineTransformMakeRotation(120 * M_PI/180); 
    } [UIView commitAnimations]; 
} 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([animationID isEqualToString:@"step1"]) { 
     [UIView beginAnimations:@"step2" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     imageView.transform = CGAffineTransformMakeRotation(240 * M_PI/180); 
     } [UIView commitAnimations]; 
    } 
    else if ([animationID isEqualToString:@"step2"]) { 
     [UIView beginAnimations:@"step3" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     imageView.transform = CGAffineTransformMakeRotation(0); 
     } [UIView commitAnimations]; 
    } 
} 

我留在原地的easeIn/easeOut曲線(而不是線性),所以你可以看到個別的動畫。

28
#import <QuartzCore/QuartzCore.h> 

CABasicAnimation *fullRotation; 
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
fullRotation.fromValue = @(0.0); 
fullRotation.toValue = @((360.0 * M_PI)/180.0); 
fullRotation.duration = 3.5f; 
fullRotation.repeatCount = MAXFLOAT; 

[view.layer addAnimation:fullRotation forKey:@"rotation-animation"]; 

如果你想改變錨點它繞那麼你可以做

CGRect frame = view.layer.frame; 
self.anchorPoint = CGPointMake(0.5, 0.5); // rotates around center 
self.frame = frame;