presentViewController transition animation presentViewController transition animation objective-c objective-c

presentViewController transition animation


Add this line of code before presenting view controller

secondController.modalTransitionStyle   = UIModalTransitionStyleCrossDissolve;secondController.modalPresentationStyle = UIModalPresentationFullScreen;// Take a look at this enumtypedef enum {   UIModalTransitionStyleCoverVertical = 0,   UIModalTransitionStyleFlipHorizontal,   UIModalTransitionStyleCrossDissolve,   UIModalTransitionStylePartialCurl,} UIModalTransitionStyle;


for Swift

Define animations types


enum UIModalTransitionStyle : Int {    case CoverVertical = 0    case FlipHorizontal    case CrossDissolve    case PartialCurl}

How to use


let vc : PageHomeViewController = storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") as! PageHomeViewControllervc.modalTransitionStyle = .FlipHorizontalself.presentViewController(vc, animated: true, completion: nil)


My decision for resolving the animation "cover horizontal" like a UINavigationViewController push method with using UIViewControllerTransitioningDelegate.

1.Create a custom transition.

Header

@interface CoverHorizontalTransition: NSObject<UIViewControllerAnimatedTransitioning>@property (assign, nonatomic) BOOL dismiss;@end

Implementation

@implementation CoverHorizontalTransition- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext{    UIViewController *fromViewController;    fromViewController =    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];    UIViewController *toViewController;    toViewController =    [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];    UIView *containerView = transitionContext.containerView;    CGRect animatedViewFrame;    animatedViewFrame = containerView.bounds;    animatedViewFrame.origin = CGPointMake(CGRectGetWidth(animatedViewFrame), 0);    [containerView addSubview:toViewController.view];    if (_dismiss) {        [containerView bringSubviewToFront:fromViewController.view];        [UIView         animateWithDuration:[self transitionDuration:transitionContext]         animations:^{             fromViewController.view.frame = animatedViewFrame;         } completion:^(BOOL finished) {             [containerView.superview addSubview:toViewController.view];             [fromViewController.view removeFromSuperview];             [transitionContext completeTransition:YES];         }];    } else {        toViewController.view.frame = animatedViewFrame;        [UIView         animateWithDuration:[self transitionDuration:transitionContext]         animations:^{             toViewController.view.center = containerView.center;         } completion:^(BOOL finished) {             [transitionContext completeTransition:YES];         }];    }}- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext{    return 0.25;}@end

2.Create transition delegate.

@implementation CustomViewControllerTransitioningDelegate- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{    return [CoverHorizontalTransition new];}- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{    CoverHorizontalTransition *transition;    transition = [CoverHorizontalTransition new];    transition.dismiss = YES;    return transition;}@end

Sample of using.

...// Save delegate to strong propertysecondController.customTransitioningDelegate =[BaseViewControllerTransitioningDelegate new];secondController.transitioningDelegate =secondController.customTransitioningDelegate;secondController.modalPresentationStyle = UIModalPresentationCustom;[self presentViewController:secondController animated:YES completion:nil];

This code works for iOS 10+.