Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何让视图永远旋转?_Iphone_Uiview_Core Animation - Fatal编程技术网

Iphone 如何让视图永远旋转?

Iphone 如何让视图永远旋转?,iphone,uiview,core-animation,Iphone,Uiview,Core Animation,有没有办法让视图以指定的速度永久旋转?我需要一个指示器之类的东西。我知道有一个奇怪的LXX00FF常量(我记不清了),它代表“永远”。你可以用巨大的值作为浮点值(如果我没记错的话,动画的repeatCount属性是浮点) 要设置动画,可以使用+animationWithKeyPath:方法创建动画对象: CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; an

有没有办法让视图以指定的速度永久旋转?我需要一个指示器之类的东西。我知道有一个奇怪的LXX00FF常量(我记不清了),它代表“永远”。

你可以用
巨大的值作为浮点值(如果我没记错的话,动画的repeatCount属性是浮点)

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

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")
我打赌:

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

我的解决方案是有点黑客,因为它不使用核心动画,但至少它真正永远工作,不需要您设置多个动画步骤

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

听起来不错!但我如何设置动画,让视图旋转390度并继续滚动?@openfrog,这是正确的答案。通过使用基本动画显式旋转视图的层,可以旋转视图。如果你按照弗拉基米尔的建议去做,它将“继续前进”。