Replace-root-view-controller custom segue with animation?

I have a custom subclass UIStoryboardSeguethat simply replaces the root view controller with the target VC. It works exactly the way I want ... however, I would like to add a transition animation, and I cannot find good examples of how to do this in the context of replacing the root VC.

The -perform selector of my class is as follows:

-(void)perform {
    UIViewController *source = (UIViewController *)self.sourceViewController;
    source.view.window.rootViewController = self.destinationViewController;
}

... how to add a nice animated transition?

+4
source share
1 answer

" ". , , , z- . .

 -(void)perform {
    CGFloat dur = 1.0;
    UIView *destView = [self.destinationViewController view];
    UIView *srcView = [self.sourceViewController view];
    CGFloat viewWidth = srcView.frame.size.width;
    CGPoint center = srcView.center;
    AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
    destView.frame = srcView.bounds;
    [appDel.window insertSubview:destView belowSubview:srcView];

    [UIView animateWithDuration:dur animations:^{
        srcView.frame = CGRectOffset(srcView.frame, -viewWidth/1.9, -20);
        destView.frame = CGRectOffset(destView.frame, viewWidth/1.9, 20); 
    } completion:^(BOOL finished) {
        [appDel.window bringSubviewToFront:destView];
        [UIView animateWithDuration:dur animations:^{
            destView.center = center;
            srcView.center = center;
        } completion:^(BOOL finished) {
            [srcView removeFromSuperview];
            appDel.window.rootViewController = self.destinationViewController;
        }];
    }];
}
+4

Source: https://habr.com/ru/post/1589062/


All Articles