I am trying to add custom transitions to my existing iOS App. It seems to keep crashing. My app has a main navigation controller, which I have sub-classed. Root controller of the navigation controller acts as delegate for the UINavigationController.
PS: I have also tried making the custom navigation controller a delegate of itself but it crashes there before the call to the animationControllerForOperation delegate method.
This is the code that I have
Custom Navigation Controller
@interface MainNavigationViewController : UINavigationController
@end
@implementation MainNavigationViewController
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if ([fromVC isKindOfClass:[FlowLayoutCategoryController class]] && [toVC isKindOfClass:[FlowLayoutCategoryController class]]) {
if (operation == UINavigationControllerOperationPush) {
CategoryTransitionAnimation *animation = [CategoryTransitionAnimation new];
animation.type = TransitionTypePush;
return animation;
}
else if (operation == UINavigationControllerOperationPop) {
CategoryTransitionAnimation *animation = [CategoryTransitionAnimation new];
animation.type = TransitionTypePop;
return animation;
}
else {
return nil;
}
}
return nil;
}
Transition Animation Class
typedef enum
{
TransitionTypePush,
TransitionTypePop
} TransitionType;
@interface CategoryTransitionAnimation : NSObject<UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) TransitionType type;
@end
#import "CategoryTransitionAnimation.h"
@implementation CategoryTransitionAnimation
#pragma mark - UIViewControllerAnimatedTransitioning
- (void) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
//Get references to the view hierarchy
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.type == TransitionTypePush) {
//Add 'to' view to the hierarchy with 0.0 scale
toViewController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
[containerView insertSubview:toViewController.view aboveSubview:fromViewController.view];
//Scale the 'to' view to to its final position
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} else if (self.type == TransitionTypePop) {
//Add 'to' view to the hierarchy
[containerView insertSubview:toViewController.view belowSubview:fromViewController.view];
//Scale the 'from' view down until it disappears
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.transform = CGAffineTransformMakeScale(0.0, 0.0);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
- (NSTimeInterval) transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.0;
}
@end