如何从故事板中的UIButton执行自定义segue(模态segue)

时间:2012-02-27 13:58:57

标签: ipad ios5 segue uistoryboardsegue

我想通过源场景中的按钮来截取目标场景。我还想控制viewcontroller之间的过渡动​​画(我想从右到左动画2个视图)。是否有可能通过替换segue这样做?我尝试替换segue和push segue但是segue没有发生任何建议我应该如何进行?谢谢!

1 个答案:

答案 0 :(得分:8)

我发现替换segue和push segue是误导性的,因为替换似乎可用于主细节控制器和仅用于导航控制器的推送segue。在这种情况下,我需要实现自定义segue。您需要子类化UIStoryboardSegue并覆盖perform segue。

以下是我的代码:

-(void)perform{
    UIView *sourceView = [[self sourceViewController] view];
    UIView *destinationView = [[self destinationViewController] view];      

    UIImageView *sourceImageView;
    sourceImageView = [[UIImageView alloc] 
                       initWithImage:[sourceView pw_imageSnapshot]];

    // force the destination to be in landscape before screenshot
    destinationView.frame = CGRectMake(0, 0, 1024, 748);
    CGRect originalFrame = destinationView.frame;
    CGRect offsetFrame = CGRectOffset(originalFrame, originalFrame.size.width, 0);


    UIImageView *destinationImageView;
    destinationImageView = [[UIImageView alloc] 
                            initWithImage:[destinationView pw_imageSnapshot]];

    destinationImageView.frame = offsetFrame;  
    [self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];

    [destinationView addSubview:sourceImageView];                        
    [destinationView addSubview:destinationImageView]; 

    void (^animations)(void) = ^ {                                     
        [destinationImageView setFrame:originalFrame];

    };

    void (^completion)(BOOL) = ^(BOOL finished) {                       
        if (finished) {

            [sourceImageView removeFromSuperview];
            [destinationImageView removeFromSuperview];

        }
    };

    [UIView animateWithDuration:kAnimationDuration delay:.0 options:UIViewAnimationOptionCurveEaseOut animations:animations completion:completion];
 }

主要思想是创建源场景和目标场景的截图视图;将它们添加到目标场景视图,控制这两个视图的动画,调用sourceviewController上的presentModalViewController函数,并在完成动画时删除两个屏幕截图视图。

可以在此链接的第15章中找到实现屏幕截图实用程序功能的示例:http://learnipadprogramming.com/source-code/