Question

I am trying to create a category for UINavigationController so I can put together a few custom animations. I came across this question and it got me started. I've come up with the following for a push function:

- (void)pushViewControllerMoveInFromBottom:(UIViewController *)viewController {
    [CATransaction begin];
    CATransition *transition;
    transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromBottom;
    transition.duration = 0.7;

    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    [[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
    [self  pushViewController:viewController animated:YES];
    [CATransaction commit];
}

I don't understand which view/layer is being accessed via the subview call. So if someone could help me understand that, that would be helpful. It seems to be effecting whatever view is about to come onto the screen.

But my main question is that I'm trying to create a pop that will "undo" the above function. For clarity sake, lets say I have two views, A and B. A is the first view that is shown on the screen. The function above, will move in B without pushing A out. Much like a modal view would. But the function below slides A back in over B, rather that sliding B up and leaving A in place.

- (void)popViewControllerMoveInFromTop {
    [CATransaction begin];
    CATransition *transition;
    transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    transition.duration = 0.7;

    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];

    [[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
    [self  popViewControllerAnimated:YES];    
    [CATransaction commit];
}

I don't want to use a modal view and I would rather not use custom animation blocks to change the frame of the views.

Was it helpful?

Solution

I've accomplished something similar, though I didn't bother to move it into it's own function:

CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[self.navigationController popViewControllerAnimated:NO]

You could abstract it out. Using your code, it would look like:

- (void)popViewControllerMoveInFromTop {
[CATransaction begin];
CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
transition.duration = 0.7;

[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];

[self.view.layer addAnimation:transition forKey:nil];
[self  popViewControllerAnimated:NO];    
[CATransaction commit];
}

You can also create a custom transition for pop using UIView's animations. Here you are applying an animation to the view that's about to disappear and then popping it from the view stack:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 

[self.navigationController popViewControllerAnimated:NO];

[UIView commitAnimations];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top