Implementing a custom transition animation for view controllers is not too difficult. Let's consider that we need to push from right/left when presenting a modal view controller, and then
pushing out to the other direction when dismissing the dialog. The below mentioned UIViewController category serves this purpose for us.
and the implementation:
To use it, you import "UIViewController+Transitions.h" in your file and call the presentModalViewController:withPushDirection:
@interface UIViewController (Transitions) - (void) presentModalViewController:(UIViewController *)modalViewController withPushDirection: (NSString *) direction; - (void) dismissModalViewControllerWithPushDirection:(NSString *) direction; @end
and the implementation:
#import "UIViewController+Transitions.h"
@implementation UIViewController (Transitions)
- (void) presentModalViewController:(UIViewController *)modalViewController withPushDirection: (NSString *) direction {
[CATransaction begin];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = direction;
transition.duration = 0.25f;
transition.fillMode = kCAFillModeForwards;
transition.removedOnCompletion = YES;
[[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[CATransaction setCompletionBlock: ^ {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
});
}];
[self presentViewController:modalViewController animated:NO completion:nil];
[CATransaction commit];
}
- (void) dismissModalViewControllerWithPushDirection:(NSString *) direction {
[CATransaction begin];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = direction;
transition.duration = 0.25f;
transition.fillMode = kCAFillModeForwards;
transition.removedOnCompletion = YES;
[[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[CATransaction setCompletionBlock: ^ {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
});
}];
[self dismissViewControllerAnimated:NO completion:nil];
[CATransaction commit];
}
@end
To use it, you import "UIViewController+Transitions.h" in your file and call the presentModalViewController:withPushDirection:
the direction parameter receives core animation transition sub-types such as kCATransitionFromRight.
You can download the category files from here and import to your project.
Enjoy...