To perform an animation simultaneously on two layers, you must add an adequate CAAnimationGroup for each level.
[nextView.layer addAnimation:nextViewAnimation forKey:nil]; [currentView.layer addAnimation:currentViewAnimation forKey:nil];
nextViewAnimation will look like this:
CAAnimationGroup *nextViewAnimation = [CAAnimationGroup animation]; NSMutableArray *nextAnimations = [NSMutableArray array]; [nextAnimations addObject:[self opacityAnimation:YES]];
and currentViewAnimation:
CAAnimationGroup *currentViewAnimation = [CAAnimationGroup animation]; NSMutableArray *currentAnimations = [NSMutableArray array]; [currentSceneAnimations addObject:[self opacityAnimation:NO]];
These methods create basic animations:
- (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; }
With a Boolean forward, you can simulate a left or right transition.
source share