【问题标题】:Replace-root-view-controller custom segue with animation?用动画替换根视图控制器自定义segue?
【发布时间】:2023-03-04 09:38:01
【问题描述】:

我有一个自定义的UIStoryboardSegue 子类,它只是将根视图控制器替换为目标 V​​C。完全按照我的意愿工作......但是,我希望能够添加过渡动画,但我找不到任何关于如何在替换根 VC 的上下文中做到这一点的好例子。

我班级的 -perform 选择器是这样的:

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

...如何添加漂亮的动画过渡?

【问题讨论】:

    标签: ios objective-c storyboard segue uistoryboardsegue


    【解决方案1】:

    您可以通过多种方式添加“漂亮的动画”。这是一种洗牌动画的示例,其中一个视图向上和向左移动,而另一个视图向下和向右移动,然后在更改两个视图的 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;
            }];
        }];
    }
    

    【讨论】:

    • 啊!好的,所以基本上我可以像其他任何 segue 一样行事,只需通过替换根视图控制器来结束它。比我想象的要容易得多!谢谢。
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多