2012-07-12 10 views
5

Quiero poder usar una UIViewAnimationCurve para rotaciones y otra para cambios en la posición. es posible?Utilice dos UIViewAnimationCurves diferentes en la misma animación

Por ejemplo (en pseudo código);

// Begin animations 

// Set rotation animation curve to EaseInOut 

// Set position animation curve to Linear 

// Make some changes to position 

// Make some change to the angle of rotation 

// Commit the animations 

EDIT: (enfoque CAAnimationGroup sugiere a continuación) - han creado 2 CABasicAnimations separadas y una CAAnimationGroup sin embargo, las animaciones no están comenzando. ¿Algunas ideas?

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToWorldSpaceFromViewSpace:self.spriteView.position]]; 
postionAnimation.delegate = self; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.angle -= M_PI_2]; 
rotationAnimation.delegate = self; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 

// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

Respuesta

3

Su mejor opción es utilizar varias llamadas a animateWithDuration: retardo: Opciones: animaciones: finalización :. Use una curva de animación diferente en cada llamada, y se ejecutarán en paralelo. O bien, con diferentes valores de retraso, puede hacer que se ejecuten en secuencia.

El código podría tener este aspecto:

[UIView animateWithDuration: .5 
    delay: 0 
    options: UIViewAnimationOptionCurveEaseInOut 
    animations: ^{ 
    view1.center = CGPointMake(x, y); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

[UIView animateWithDuration: .5 
    delay: .5 
    options: UIViewAnimationOptionCurveLinear 
    animations: ^{ 
    view2.center = CGPointMake(x2, y2); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

Si utiliza CAAnimationGroups, hay varios problemas.

En primer lugar, el grupo de animación determina la curva de animación para todo el grupo. No creo que pueda especificar curvas diferentes para cada sub animación (aunque confieso que no lo he probado).

En segundo lugar, si agrega animaciones a un grupo, los delegados a las animaciones individuales no obtienen llamado. Solo se llaman los métodos de delegado del grupo de animación.

+0

Este es un gran consejo, gracias Duncan. Lo intentaré mañana. Dave –

+0

Hola Duncan, ¡hazlo y trabaja! La única pregunta que tengo es, ¿está bien llamar a mi método [self transformPointToViewSpaceFromWorldSpace: self.spriteView.position] dentro del bloque? Funciona y los instrumentos no muestran ninguna fuga, sin embargo, recuerdo vagamente algo acerca de usar uno mismo dentro de los bloques. Gracias de nuevo, Dave. –

4

Intenta crear dos animaciones diferentes. Con el método de UIView no puedes establecer diferentes animacionesCurvas para diferentes propiedades de tu animación. O puede divertirse con CAAnimations y crear un CAAnimationGroup en el que establece sus dos CABasicAnimation

+0

Gracias iSofTom, he intentado este enfoque (ver edición en cuestión) pero según los métodos delegados mis animaciones no están comenzando. ¿Algunas ideas? –

0

Ok, finalmente lo resolvió. Gracias por la dirección iSofTom (ha votado su respuesta). Había establecido los delegados de las animaciones individuales, pero no el grupo.

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.fromValue = [NSValue valueWithCGPoint:self.spriteView.center]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]]; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat:self.spriteView.angle]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.rotation]; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 
animationsGroup.delegate = self; 
// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

y

- (void)animationDidStart:(CAAnimation *)theAnimation { 
radiansToDegrees(self.spriteView.rotation)); 
    self.spriteView.angle = self.spriteView.rotation; 

NSStringFromCGPoint(self.spriteView.position)); 
    self.spriteView.center = [self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]; 
} 
Cuestiones relacionadas