如何让视图永远旋转?

时间:2010-09-27 12:35:50

标签: iphone uiview core-animation

有没有办法让视图以指定的速度永远旋转?我需要一个指标类型的东西。我知道有一个奇怪的Lxxxxx00ff常数(不记得它)代表“永远”。

3 个答案:

答案 0 :(得分:20)

您可以使用HUGE_VAL作为浮动值(如果我没记错的话,动画的repeatCount属性是浮点数)。

要设置动画,您可以使用+animationWithKeyPath:方法创建CAAnimation对象:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 3.0f;
animation.repeatCount = HUGE_VAL;
[rotView.layer addAnimation:animation forKey:@"MyAnimation"];

如果我记得只使用UIView正确创建这种旋转动画是不可能的,因为360度旋转(2 * M_PI弧度)被优化为根本没有旋转。

答案 1 :(得分:2)

我的解决方案有点hacky,因为它不使用核心动画,但至少它真正永远,并且不需要你设置多个动画步骤。

...
// runs at 25 fps
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0/25
                          target:self
                          selector:@selector(rotate)
                          userInfo:nil
                          repeats:YES];
[timer fire];
...

- (void)rotate {
    static int rotation = 0;

    // assuming one whole rotation per second
    rotation += 360.0 / 25.0;
    if (rotation > 360.0) {
        rotation -= 360.0;
    }
    animatedView.transform = CGAffineTransformMakeRotation(rotation * M_PI / 180.0);
}

答案 2 :(得分:0)

我的赌注是:

-(void)animationDidStopSelector:... {
  [UIView beginAnimations:nil context:NULL];
  // you can change next 2 settings to setAnimationRepeatCount and set it to CGFLOAT_MAX
  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(animationDidStopSelector:...)];
  [UIView setAnimationDuration:...

  [view setTransform: CGAffineTransformRotate(CGAffineTransformIdentity, 6.28318531)];

  [UIView commitAnimations];
}

//start rotation
[self animationDidStopSelector:...];

好的打赌:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationRepeatCount: CGFLOAT_MAX];
[UIView setAnimationDuration:2.0f];

[view setTransform: CGAffineTransformMakeRotation(6.28318531)];

[UIView commitAnimations];