0

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.

enter image description here

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
aniruddhc
  • 320
  • 1
  • 3
  • 15
  • If it's crashing then your code is wrong. But you don't show any code, so no one can what the problem is. Would you like to see some custom transition code that works? I've got quite a bit of it in my github site: https://github.com/mattneub/Programming-iOS-Book-Examples Look at the 6 examples starting at bk2ch06p292customAnimation1 – matt Feb 03 '14 at 04:31
  • I have added my code. I tried adding breakpoints in my code but the app crashes before getting into any of my code. – aniruddhc Feb 07 '14 at 19:54
  • It is crashing _somewhere_ in your code; a project without code does not crash. Create a global Exception breakpoint so that you can catch the exception at the point where it is thrown. – matt Feb 07 '14 at 20:14
  • By the way it appears to be crashing in your `animateTransition:` method. So you can set a breakpoint at the start of it and walk through to find the place where it is crashing. Your code looks pretty straightforward so I'm sure we can find the spot! – matt Feb 07 '14 at 20:19

1 Answers1

0

I'm going to make a guess that the problem is that you have omitted a step: You are supposed to fetch the finalFrameForViewController: for the "to" view controller and set the view's frame to that. Just a guess, but I can't help noticing that you are not giving the view any frame, which can't be good.

matt
  • 515,959
  • 87
  • 875
  • 1,141