关于旋转的动画

旋转的动画:http://dev.son1c.com/show/2565.html

最主要的是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  关键是completion(^(BOOL finished){})里的finished变量,它代表着整个动画是否是真正结束。


最开始的想法是让旋转的弧度从0到2 * M_PI,  让动画不停的repeat,实验了一下,没有任何效果,系统动画的时候看到0与2 *M_PI是同一起一始点,所以没有效果。

后来想到一种办法,就是一个变量不断的累加变大,这样影响弧度也随着变化,就达到了圆周运动的效果。


直接上代码:

  1. -(void) startAnimation  
  2. {  
  3.     [UIView beginAnimations:nil context:nil];  
  4.     [UIView setAnimationDuration:0.01];  
  5.     [UIView setAnimationDelegate:self];  
  6.     [UIView setAnimationDidStopSelector:@selector(endAnimation)];  
  7.     imageView.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));  
  8.     [UIView commitAnimations];  
  9. }  
  10.   
  11. -(void)endAnimation  
  12. {  
  13.     angle += 10;  
  14.     [self startAnimation];  
  15. }  

当然你可以用block的方式写也是一样的。

  1. - (void)startAnimation  
  2. {  
  3.     CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));  
  4.       
  5.     [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{  
  6.         imageView.transform = endAngle;  
  7.     } completion:^(BOOL finished) {  
  8.         angle += 10; [self startAnimation];  
  9.     }];  
  10.       
  11. }  


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;

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值