使用计时器逐渐旋转UIImageView

时间:2014-08-27 08:05:15

标签: ios uiimageview rotation

我正在尝试使用计时器逐渐旋转UIImageView,此代码将其旋转,但它似乎不会围绕图像中心旋转,以及将CGAffineTransformMakeRotation添加到我的代码中似乎停止了" main"中的所有其他代码方法

#define radians(degrees) (degrees * M_PI/180)

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.016 target:self selector:@selector(main) userInfo:nil repeats:YES];
-(void)main{
rotate++;
uiImageView.transform = CGAffineTransformMakeRotation(radians(rotate));
uiImageView.center = CGPointMake(uiImageView.center.x+1, uiImageView.center.y);
}

3 个答案:

答案 0 :(得分:3)

试试这个:

启动动画:

  CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  rotation.fromValue = [NSNumber numberWithFloat:0];
  rotation.toValue = [NSNumber numberWithFloat:(2*M_PI)];
  rotation.duration = 1.0;// Speed
  rotation.repeatCount = HUGE_VALF;// Repeat forever.
  [imgViewCube.layer addAnimation:rotation forKey:@"Spin"];

停止动画:

  [imgViewCube.layer removeAnimationForKey:@"Spin"];

答案 1 :(得分:0)

你可以慢慢旋转指定动画的持续时间

 NSTimeInterval duration = 1.5;
[UIView animateWithDuration:duration animations:^
{
    // set the final transform/freame/center here
    uiImageView.transform = CGAffineTransformMakeRotation(radians(rotate));
    uiImageView.center = CGPointMake(uiImageView.center.x+1, uiImageView.center.y);

} completion:^(BOOL finished)
 {
     NSLog(@"animation completed");
 }];

答案 2 :(得分:0)

你可以做这样的事情

// this spin completes 360 degrees every 2 seconds
    [UIView animateWithDuration:1.0f
                          delay:0.0f
                        options: UIViewAnimationOptionRepeat
                     animations: ^(void){
                         uiImageView.transform = CGAffineTransformRotate(uiImageView.transform, M_PI / 2);
                     }
                     completion:NULL];