2014-08-27 34 views
1

我试图旋转UIImageView逐渐使用计时器,此代码旋转它,但它似乎不像它的图像中心旋转,以及将CGAffineTransformMakeRotation添加到我的代码似乎停止所有其他代码中的“主”的方法旋转UIImageView逐渐使用计时器

#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

试试这个:

开始动画:

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"]; 
+0

感谢这个工作几乎完美,但是当我停止动画图像可以追溯到它的原始状态,无论如何保持它在旋转状态? – NickPali 2014-08-27 09:20:12

+0

'addAnimation:'方法的作用是将效果添加到对象'presentationLayer'中。正因为如此,当我们删除动画时,视图将回到其初始状态。假设您想要将对象从A点移动到B点,并在动画结束时保持已更改的状态,则应先将图层位置更改为B点,然后制作一个动画,显示从A点移动到B点的动画。 - 继续 – croyneaus4u 2014-08-27 09:57:24

+0

- 继续 - >但在你的情况下,我不认为这是可以做到的。所以在停止动画时,我会建议从ImageView的PresentationLayer获取当前值并将其提供给ImageView的图层。在相同的方法。干杯。 – croyneaus4u 2014-08-27 09:57:57

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"); 
}]; 
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];