关于动画

最开始的想法是让旋转的弧度从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. }  
-(void) startAnimation
{
	[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)endAnimation
{
	angle += 10;
	[self startAnimation];
}


当然你可以用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. }  
- (void)startAnimation
{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));
    
    [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageView.transform = endAngle;
    } completion:^(BOOL finished) {
        angle += 10; [self startAnimation];
    }];
    
}

 

 

PS:其中angle是double类型。

 

这下就可以360度旋转了。

 

还有一种方法:

 

  1. CABasicAnimation* rotationAnimation;  
  2. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  3. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];  
  4. rotationAnimation.duration = duration;  
  5. rotationAnimation.cumulative = YES;  
  6. rotationAnimation.repeatCount = repeat;  
  7.   
  8. [_loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];  
posted @ 2012-10-08 17:58  gagag  阅读(661)  评论(0)    收藏  举报