如何从故事板中的UIButton执行自定义segue(模态segue) [英] How do you perform a customized segue (modal segue) from a UIButton in storyboard

查看:107
本文介绍了如何从故事板中的UIButton执行自定义segue(模态segue)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to segue a destination scene through a button present in source scene. I also would like to have a control of the transition animation between the viewcontroller (I want to animate the 2 views from right to left). Is it possible to do so through the replace segue? I try both the replace segue and the push segue but the segue is not happening any suggestion how i should proceed there? Thanks!

推荐答案

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

I found out that the replace segue and push segue were misleading because the replace seems to be available for a master detail controller and the push segue for a navigation controller only. In this case I needed to implement a customize segue instead. You need to subclass the UIStoryboardSegue and override the 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函数,并在完成动画时删除两个屏幕截图视图。

The main idea is to create a screenshot view of the source and destination scene; add them to the destination scene view, control the animation of those two views, call to the presentModalViewController function on the sourceviewController and remove the two screenshots views when done with the animation.

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

One can found an example of implementing a screenshot utility function here in Ch15 of this link: http://learnipadprogramming.com/source-code/

这篇关于如何从故事板中的UIButton执行自定义segue(模态segue)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆