Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

presentViewController transition animation

In my code I am using presentViewController to call my second viewcontroller

[self presentViewController:secondController animated:YES completion:nil];

When I call I need to show left to right animation (like in navigationController)

I don't want to use the navigationController but I need the animation similar to navigationController in presentViewController...

like image 983
Subramanian Raj Avatar asked Mar 29 '15 14:03

Subramanian Raj


People also ask

What is presentViewController?

The presentViewController:animated:completion: method always displays the view controller modally. The view controller that calls this method might not ultimately handle the presentation but the presentation is always modal. This method adapts the presentation style for horizontally compact environments.

How to present view controller in animation swift?

Let's Get Started storyboard file. Next, Drag and Drop a button in your view controller, give it the title “Present Second VC”. Next, add another View Controller in your storyboard, and drag-drop a button in it, give it the title “Dismiss View”. Tweak the background colors of Buttons and Views if you like.

What is a navigation controller Swift?

A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.


2 Answers

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 property
secondController.customTransitioningDelegate =
[BaseViewControllerTransitioningDelegate new];

secondController.transitioningDelegate =
secondController.customTransitioningDelegate;
secondController.modalPresentationStyle = UIModalPresentationCustom;

[self presentViewController:secondController animated:YES completion:nil];

This code works for iOS 10+.

like image 103
Илья Еловиков Avatar answered Oct 29 '22 23:10

Илья Еловиков


Add this line of code before presenting view controller

secondController.modalTransitionStyle   = UIModalTransitionStyleCrossDissolve;
secondController.modalPresentationStyle = UIModalPresentationFullScreen;

// Take a look at this enum

typedef enum {
   UIModalTransitionStyleCoverVertical = 0,
   UIModalTransitionStyleFlipHorizontal,
   UIModalTransitionStyleCrossDissolve,
   UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
like image 45
Ch0k0l8 Avatar answered Oct 29 '22 22:10

Ch0k0l8