最近做到了图片旋转的需求,发现一篇不错的关于动画旋转的文章,转载一下:
原文地址:http://blog.csdn.net/fanxiaochuan/article/details/9348293
最主要的是CGAffineTransform 这个是动画变换的基石。
还有就是如下这段英文:
if you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explictly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.
简单区分了下CAAnimation 和 UIView直接动画的区别。
还有暂停这段动画的方法:http://stackoverflow.com/questions/17681050/how-to-cancel-the-uiview-rotate-block-animations 关键是
最开始的想法是让旋转的弧度从0到2 * M_PI, 让动画不停的repeat,实验了一下,没有任何效果,系统动画的时候看到0与2 *M_PI是同一起一始点,所以没有效果。
后来想到一种办法,就是一个变量不断的累加变大,这样影响弧度也随着变化,就达到了圆周运动的效果。
直接上代码:
- void
{ - [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.01];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(endAnimation)];
- imageView.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
- [UIView commitAnimations];
- }
- -(void
{ - angle += 10;
- [self startAnimation];
- }
当然你可以用block的方式写也是一样的。
- void
{ - CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));
- [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
- imageView.transform = endAngle;
- } completion:^(BOOL
angle += 10; [self startAnimation]; - }];
- }
PS:其中angle是double类型。
2013-07-17
22:31:10.898 CircleTest[605:c07] label = <UILabel: 0x75591c0; frame =
(46.4184 41.6974;
107.163 66.6052); transform = [-0.984808, -0.173648, 0.173648,
-0.984808, 0, 0]; text = ‘hello world‘; clipsToBounds = YES;
userInteractionEnabled = NO; layer = <CALayer: 0x75592a0>>
2013-07-17
22:31:16.266 CircleTest[605:c07] label = <UILabel: 0x75591c0; frame
= (62.9109 20.2332; 74.1782 109.534); transform = [0.258819, -0.965926,
0.965926, 0.258819, 0, 0]; text = ‘hello world‘; clipsToBounds = YES;
userInteractionEnabled = NO; layer = <CALayer: 0x75592a0>>
在旋转过程中label的frame也在不断的发生着变化。
附加:
CABasicAnimation animationWithKeyPath Types
When using the ‘CABasicAnimation’ from the
QuartzCore Framework in Objective-C, you have to specify an
animationWithKeyPath. This is a long string and is not easily listed in
the CABasicAnimation, CAPropertyAnimation, or the CAAnimation
class.
I ended up finding a handy chart within the Core Animation Programming guide in Apple’s iPhone OS Reference Library.
这下就可以360度旋转了。
还有一种方法:
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat;
[_loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
还可以用这个方法找到这个动画,动态的对动画属性进行改变,而且如果你使用的是组合动画,那么这个方法会动态的改变你的某个动画效果,从而让你的动画变的更加牛b。But 我刚刚试了试,好像不能对这些已有的属性进行修改。rotationAnimation 这个你是改不了的。
【label.layeranimationForKey:@"rotationAnimation"】
但是你可以进行移除操作如下:
[label.layerremoveAnimationForKey:@"rotationAnimation"];
暂停动画
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [label.layer
convertTime:CACurrentMediaTime()
fromLayer:nil];
label.layer.speed =
0.0;
label.layer.timeOffset = pausedTime;
}
移除动画
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [label.layer
timeOffset];
label.layer.speed =
1.0;
label.layer.timeOffset =
0.0;
label.layer.beginTime =
0.0;
CFTimeInterval timeSincePause = [label.layer
convertTime:CACurrentMediaTime()
fromLayer:nil] - pausedTime;
label.layer.beginTime = timeSincePause;
}