Reputation: 524
I created a custom zoom-in transition when pushing a new viewController, and it used to work perfectly fine. Now I want to create a zoom-out effect when popping the viewController, and even-thought the final state is correct, the animation is wrong since I don't know how to identify if it is pushing or popping and all the methods like isBeingPresented
return false and presentedViewController
is always nil
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
self.transitionContext = transitionContext;
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = (UIViewController *) [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[containerView addSubview:toViewController.view];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration = [self transitionDuration:transitionContext];
scaleAnimation.delegate = self;
scaleAnimation.removedOnCompletion = YES;
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.duration = [self transitionDuration:transitionContext];
opacityAnimation.delegate = self;
opacityAnimation.removedOnCompletion = YES;
if (toViewController.isBeingPresented) {
scaleAnimation.fromValue = [NSNumber numberWithDouble:0];
scaleAnimation.toValue = [NSNumber numberWithDouble:1];
opacityAnimation.fromValue = [NSNumber numberWithFloat:1];
opacityAnimation.toValue = [NSNumber numberWithFloat:0];
} else {
scaleAnimation.fromValue = [NSNumber numberWithDouble:1];
scaleAnimation.toValue = [NSNumber numberWithDouble:0];
opacityAnimation.fromValue = [NSNumber numberWithFloat:0];
opacityAnimation.toValue = [NSNumber numberWithFloat:1];
}
[toViewController.view.layer addAnimation:scaleAnimation forKey:nil];
[fromViewController.view.layer addAnimation:opacityAnimation forKey:nil];
}
Upvotes: 0
Views: 1802
Reputation: 1000
See my variant of push viewController with pop animation
- (void)navigationController:(UINavigationController *)navigationController
present:(UIViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion {
self.completion = completion;
if (animated) {
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[animation setDuration:kPresentAnimationDuration];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[navigationController.view.layer addAnimation:animation forKey:kCATransition];
}
[navigationController pushViewController:viewController animated:NO];
if (!animated) {
self.completion(YES);
}
}
Upvotes: 0
Reputation: 4550
This is not associated with your code above, but this will override the default animation of navigationController..
//.h
@interface YJKit_Navigation : UINavigationController
+ (void)navigation:(UINavigationController *)navigationController withViewController:(UIViewController *)viewController push:(BOOL)isPush;
@end
//.m
@implementation YJKit_Navigation
+ (void)navigation:(UINavigationController *)navigationController withViewController:(UIViewController *)viewController push:(BOOL)isPush
{
[navigationController.view.layer addAnimation: isPush ? PushAnimation : PopAnimation forKey:nil];
isPush ? [navigationController pushViewController:viewController animated:NO] : [navigationController popViewControllerAnimated:NO];
}
@end
and using it like:
[YJKit_Navigation navigation:YourUINavigationController withViewController:YourTargetUIViewController push:(BOOL)];
Hope this will also help you.. :)
Upvotes: 0
Reputation: 10479
You can save a UINavigationControllerOperation
variable:
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC {
self.navigationOperation = operation;
return self;
}
then you check it is push or pop:
if (self.navigationOperation == UINavigationControllerOperationPush) {
// push
} else if (self.navigationOperation == UINavigationControllerOperationPop) {
// pop
}
Upvotes: 2