Search code examples
iphoneobjective-ccore-animation

CATransition for left and right animation in the navigation stack?


I need to call one of two different CATranistion functions (I've created) one which does kCATransitionFromRight and another which does kCATransitionFromLeft, yeah I'll combine the function when I'm done.

However, I have a view in my navigation stack which is 1 view deep. Depending on whether you enter or leave the view from the view above or below in the stack, I need to use either left or right.

First view => problem view, transition left.
Problem view => first view, transition right.

Problem view => third view, transition left.
Third view <= problem view, transition right.

I currently call the functions in viewWillAppear.

However, I don't know how to tell the function which direction to use.

I know I could use an app delegate variable, singleton or nsuserdefault setting, I just wondered if there's an approach I hadn't considered which would be more appropriate?

I know I could call the function within back buttons etc, but the call within viewWillAppear will overwrite this.

+ (void)transitionFromRight:(UIViewController*)view {

    CATransition *navTransition = [CATransition animation];
    navTransition.duration = 0.65;
    navTransition.timingFunction = [CAMediaTimingFunction 
          functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [navTransition setType:kCATransitionReveal];
    [navTransition setSubtype:kCATransitionFromRight];
    [view.navigationController.navigationBar.layer 
          addAnimation:navTransition forKey:nil];

}

Solution

  • I'm assuming you're using a UINavigationController.

    None of the options you suggest are appropriate because they're all variants on the idea of global data access point, which you shouldn't need for something like this.

    Here's a better way:

    Create a UINavigationControllerDelegate for your UINavigationController. You'll get to hear about new views being pushed/popped onto the nav stack via one of the delegate methods such as navigationController:willShowViewController:animated:. In the appropriate delegate method you can set a property on your custom UIViewController so that it knows which animation to use in viewWillAppear etc.