如何在不使用OpenGL ES的情况下,使UIView
, UIImageView
或CALayer
沿着360度旋转的动画?
如何在不使用OpenGL ES的情况下,使UIView
, UIImageView
或CALayer
沿着360度旋转的动画?
#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;
我想您的意思是希望对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
曲线(而不是线性的),这样你就可以看到单独的动画效果。