2010-09-27 17 views
5

¿Hay alguna manera de permitir que una vista gire para siempre, con una velocidad especificada? Lo necesito para un tipo de indicador. Sé que existe esta extraña constante Lxxxxx00ff (no lo recuerdo exactamente) que significa "para siempre".¿Cómo dejar girar una vista para siempre?

Respuesta

20

Puede usar HUGE_VAL para el valor flotante (si mal no recuerdo, la propiedad repeatCount para la animación es un flotador).

Para animación de la configuración, puede crear el objeto CAAnimation usando +animationWithKeyPath: método:

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

si no recuerdo crear correctamente este tipo de rotación usando sólo animaciones UIView es imposible debido a las rotaciones de 360 ​​grados (2 * M_PI radianes) son optimizado para ninguna rotación en absoluto.

+0

suena bien! pero, ¿cómo podría configurar la animación para que la vista gire completamente 390 grados y seguir rodando? – openfrog

+0

@openfrog, esta es la respuesta correcta. Puede rotar la vista rotando explícitamente la capa de la vista con una animación básica. Continuará "rodando" si haces lo que Vladimir ha sugerido. –

0

mi apuesta es:

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

bien mejor apuesta:

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

[view setTransform: CGAffineTransformMakeRotation(6.28318531)]; 

[UIView commitAnimations]; 
2

mi solución para esto es un poco hacky, ya que no utiliza la animación núcleo, pero al menos funciona realmente para siempre y no requiere que configure varios pasos de animación.

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