Come si ruota UIView
, UIImageView
o CALayer
animato a 360 gradi senza utilizzare OpenGL ES?Come ruotare UIView, UIImageView o CALayer animati 360˚?
5
A
risposta
3
Presumibilmente si intende animare la rotazione di un UIImage
intorno a 360 °? Nella mia esperienza, il modo per compiere una rotazione completa cerchio è a catena più animazioni insieme:
- (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];
}
}
ho lasciato la curva (invece che lineare) al posto easeIn/easeOut
, in modo da poter vedere le singole animazioni.
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"];
Se si desidera cambiare il punto di ancoraggio a ruota attorno allora si può fare
CGRect frame = view.layer.frame;
self.anchorPoint = CGPointMake(0.5, 0.5); // rotates around center
self.frame = frame;
Ti rendi conto che la rotazione 360˚ vi darà la stessa identica immagine, giusto? – SteamTrout
Trovo triste che ho iniziato a provare a pensare a una soluzione prima che me ne rendessi conto. – jtbandes
@Steam Trout: solo se si desidera che sia istantaneo. – JeremyP