简体   繁体   中英

iOS - Creating a custom Transition animation

I am trying to create a custom transition between to view controllers that are linked to a Container View Controller with custom segues.

This is my attempt at creating the custom transition. However, what happens is the view animates up and down as intended. But the view controllers switch instantly. What I am trying to create is the view sliding off screen vertically, then the container switches view controllers, then it slides back up screen.

Any ideas how I can achieve this animation with a custom animation?

- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];



    [self transitionFromViewController:fromViewController toViewController:toViewController duration:.4 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.view.frame = CGRectMake(0, 560, self.view.frame.size.width, self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        } completion:^(BOOL finished) {
            [fromViewController removeFromParentViewController];
            [toViewController didMoveToParentViewController:self];
        }];
    }];
}

[UIViewController transitionFromViewController:toViewController] works only when both fromViewController and toViewController are already the child controllers of the receiver.


The documentation for transitionFromViewController:toViewController: says the following:

Transitions between two of the view controller's child view controllers.

fromViewController: A view controller whose view is currently visible in the parent's view hierarchy.

toViewController: A child view controller whose view is not currently in the view hierarchy.


I didn't test it out however, I think this would work:

- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);

    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    toViewController.view.frame = CGRectMake(0.0, height, width, height);
    [self.view addSubView:toViewController.view];

    [UIView transitionWithView:self.view
                  duration:0.4
                   options:UIViewAnimationOptionCurveLinear
                animations:^{
                    fromViewController.view.frame = CGRectMake(0.0, height, width, height);
                    toViewController.view.frame = CGRectMake(0.0, 0.0, width, height);
                }
                completion:^(BOOL finished) {
                    [fromViewController removeFromParentViewController];
                    [fromViewController.view removeFromSuperView];
                    [toViewController didMoveToParentViewController:self];
                }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM