【问题标题】:Subview added in custom segue got missing when parent view appears出现父视图时,在自定义 segue 中添加的子视图丢失
【发布时间】:2013-10-08 22:00:17
【问题描述】:

我正在使用 A 视图和 B 视图之间的缩放动画实现自定义转场。我的想法描述如下。

当segue从A到B时:

  1. 保存B视图的快照图像,将此图像视图作为A的子视图添加到A视图中

  2. 执行假图像视图的放大动画(它就像 B 视图越来越大,直到填满整个屏幕)

  3. 当放大动画完成后,使用导航控制器推送没有动画的真实 B 视图,并从 A 视图中移除假图像视图

当 segue 从 B 转到 A(展开)时:

  1. 保存B视图的快照图像,将其作为A的子视图添加到A视图中并放在前面

  2. 使用导航控制器没有动画的 Pop B 视图

  3. 执行假图像视图的缩小动画(它的作用就像 B 视图越来越小,直到它太小而无法看到)

它在 A 到 B 的情况下工作正常,而在 B 到 A 的情况下,在第 2 步之后,真正的 B 视图应该消失了,并且在 A 视图之上有一个 B 的假图像视图。问题来了,如果 B 的假图像视图在第 3 步之后没有从 A 视图的子视图中删除,当 A 视图出现时,B 应该仍然存在于 A 的子视图中,但似乎这个子视图已经消失了。

我在这里发现了同样的问题:View transition doesn't animate during custom pop segue 但没有人回答。

那么,大家有什么想法吗?

【问题讨论】:

    标签: objective-c segue


    【解决方案1】:

    我不确定,但我认为您不应该在来自 B 时向 A 添加视图,问题可能就在那里。而是在 B 上添加两个视图。

    此代码有效:

        //ZoomInSegue.m
        - (void)perform {
            UIViewController* source = (UIViewController *)self.sourceViewController;
            UIViewController* destination = (UIViewController *)self.destinationViewController;
    
            //Custom method to create an UIImage from a UIView
            UIImageView * destView = [[UIImageView alloc] initWithImage:[self imageWithView:destination.view]];             
    
            CGRect destFrame = destView.frame;
            destFrame.origin.x = destination.view.frame.size.width/2;
            destFrame.origin.y = destination.view.frame.size.height/2;
            destFrame.size.width = 0;
            destFrame.size.height = 0;
            destView.frame = destFrame;
    
            destFrame = source.view.frame;
    
            [source.view addSubview:destView];
    
            [UIView animateWithDuration:1.0
                             animations:^{
                                 destView.frame = destFrame;
                             }
                             completion:^(BOOL finished) {
                                 [destView removeFromSuperview];
                                 [source.navigationController pushViewController:destination animated:NO];
                             }];
        }
    
        //ZoomOutSegue.m
        - (void)perform {
            UIViewController* source = (UIViewController *)self.sourceViewController;
            UIViewController* destination = (UIViewController *)self.destinationViewController;
    
            //Custom method to create an UIImage from a UIView
            UIImageView* sourceView = [[UIImageView alloc] initWithImage:[self imageWithView:source.view]]; 
    
            CGRect sourceFrame = sourceView.frame;
            sourceFrame.origin.x = source.view.frame.size.width/2;
            sourceFrame.origin.y = source.view.frame.size.height/2;
            sourceFrame.size.width = 0;
            sourceFrame.size.height = 0;
    
            [source.view addSubview:destination.view];
            [source.view addSubview:sourceView];
    
            [UIView animateWithDuration:1.0
                             animations:^{
                                 sourceView.frame = sourceFrame;
                             }
                             completion:^(BOOL finished) {
                                 [source.navigationController popViewControllerAnimated:NO];
                             }];
        }
    

    【讨论】:

    • 非常感谢@Odrakir,您的建议有效。我做了更多的试验,似乎在自定义 segue 中,弹出的控制器实例与 segue 的目标控制器实例不同。这可以解释为什么 segue 中的目标视图在弹出后最终显示在屏幕上时已经消失了,但是 segue 和导航控制器背后的机制仍然让我感到困惑:为什么 segue 需要实例化一个新的目标控制器但它保留目标视图的视图弹出时的状态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多