How to let a view rotate forever?

风格不统一 提交于 2019-12-18 06:13:14

问题


Is there a way to let a view rotate forever, with an specified speed? I need that for an indicator kind of thing. I know there is this weird Lxxxxx00ff constant (don't remember it exactly) that stands for "forever".


回答1:


You can use HUGE_VAL for floating value (if I remember correctly, repeatCount property for animation is a float).

To setup animation you can create CAAnimation object using +animationWithKeyPath: method:

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

If I remember correctly creating this kind of rotation using just UIView animations is impossible because rotations on 360 degrees (2*M_PI radians) are optimized to no rotation at all.




回答2:


my solution for this is a bit hacky since it doesn't use core animation but at least it works truly forever and doesn't require you to setup multiple animation steps.

...
// 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);
}



回答3:


my bet is:

-(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:...];

ok better bet:

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

[view setTransform: CGAffineTransformMakeRotation(6.28318531)];

[UIView commitAnimations];


来源:https://stackoverflow.com/questions/3803792/how-to-let-a-view-rotate-forever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!