如何让视图永远旋转?

问题描述 投票:5回答:3

是否有一种方法可以让视图以指定的速度永久旋转?对于指标性的东西,我需要它。我知道有一个古怪的Lxxxxx00ff常数(记不清了)代表“永远”。

iphone uiview core-animation
3个回答
22
投票

您可以将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弧度)已优化为完全不旋转。


编辑:添加了Swift版本。

    let animation = CABasicAnimation(keyPath: "transform.rotation.z")
    animation.fromValue = NSNumber(value: 0.0)
    animation.toValue = NSNumber(value: 2*Double.pi)
    animation.duration = 3.0
    animation.repeatCount = Float.greatestFiniteMagnitude
    rotView.layer.add(animation, forKey: "MyAnimation")

2
投票

我对此的解决方案有点笨拙,因为它不使用核心动画,但至少可以真正地实现[[forever,并且不需要您设置多个动画步骤。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.