2010-08-07 4 views
5

Comment faire pivoter UIView, UIImageView ou CALayer animé 360 degress sans utiliser OpenGL ES?Comment faire pivoter UIView, UIImageView ou CALayer 360˚?

+3

Vous vous rendez compte que la rotation 360˚ vous donnera exactement la même image à droite? – SteamTrout

+0

Je trouve triste que j'ai commencé à essayer de penser à une solution avant de m'en rendre compte. – jtbandes

+0

@Truit à la vapeur: seulement si vous voulez qu'il soit instantané. – JeremyP

Répondre

3

Vraisemblablement, vous voulez dire animer en tournant un UIImage autour d'un 360 °? Dans mon expérience, la façon d'accomplir une rotation complète du cercle est à chaîne multiples animations ensemble:

- (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]; 
    } 
} 

J'ai quitté la courbe easeIn/easeOut (au lieu de linéaire) en place, afin que vous puissiez voir les animations individuelles.

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

Si vous voulez changer le point d'ancrage à tourner autour alors vous pouvez faire

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