Con el fin de ejecutar las animaciones simultáneamente sobre las dos capas, debe agregar el CAAnimationGroup adecuada para cada capa.
[nextView.layer addAnimation:nextViewAnimation forKey:nil];
[currentView.layer addAnimation:currentViewAnimation forKey:nil];
nextViewAnimation sería:
CAAnimationGroup *nextViewAnimation = [CAAnimationGroup animation];
NSMutableArray *nextAnimations = [NSMutableArray array];
[nextAnimations addObject:[self opacityAnimation:YES]]; // fade in
CGPoint fromPoint = CGPointMake(forward ? nextView.center.x + nextView.frame.size.width : nextView.center.x - nextView.frame.size.width, nextView.center.y);
[nextAnimations addObject:[self positionAnimationFromPoint:fromPoint toPoint:nextView.center]]; // traslation in
nextViewAnimation.animations = nextAnimations;
y currentViewAnimation:
CAAnimationGroup *currentViewAnimation = [CAAnimationGroup animation];
NSMutableArray *currentAnimations = [NSMutableArray array];
[currentSceneAnimations addObject:[self opacityAnimation:NO]]; // fade out
CGPoint toPoint = CGPointMake(forward ? currentView.center.x - currentView.frame.size.width : currentView.center.x + currentView.frame.size.width, currentView.center.y);
[currentAnimations addObject:[self positionAnimationFromPoint:currentView.center toPoint:toPoint]]; // traslation out
currentViewAnimation.animations = currentAnimations;
Estos métodos crean las animaciones básicas:
- (CABasicAnimation *)opacityAnimation:(BOOL)fadeIn {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"opacity"];
a.fromValue = [NSNumber numberWithFloat:fadeIn ? 0.0 : 1.0];
a.toValue = [NSNumber numberWithFloat:fadeIn ? 1.0 : 0.0];
return a;
}
- (CABasicAnimation *)positionAnimationFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"position"];
a.fromValue = [NSValue valueWithCGPoint:fromPoint];
a.toValue = [NSValue valueWithCGPoint:toPoint];
return a;
}
Con el booleano adelante puede simular la transición "desde la izquierda" o "desde la derecha".
Gracias Joris. No es la vista A la que reemplaza la vista B. Es el estado original de la vista que se reemplaza por el nuevo estado. – hpique
Lo siento, de alguna manera estoy usando la palabra vista, donde debería haber usado CALayer. De hecho, una transición funciona en los contenidos de View: no conozco la implementación de Apple, podría ser que OpenGL sea compatible con la animación de contenido de CALayer. La mejor manera de hacerlo usted mismo sería la forma descrita anteriormente aplicada a las subcapas. –
¿Cómo se diferencia entre la subcapa A y la subcapa B? CATransition parece hacer esto automágicamente. – hpique