当父视图出现时,在自定义segue中添加的子视图丢失了

时间:2013-10-08 13:16:09

标签: objective-c segue

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

当segue从A到B时:

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

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

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

  4. 当segue从B到A(放松)时:

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

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

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

    4. 在A到B的情况下工作正常,而在B到A的情况下,在步骤2之后,真实的B视图应该消失,并且在A视图的顶部存在B的假图像视图。问题是,如果在步骤3之后未从A视图的子视图中删除B的假图像视图,则当A视图出现时,B应该仍然存在于A的子视图中,但看起来这个子视图已经消失。

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

      那么,有人有什么想法吗?

1 个答案:

答案 0 :(得分: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];
                         }];
    }
相关问题