您如何从情节提要中的 UIButton 执行自定义转场(模态转场) [英] How do you perform a customized segue (modal segue) from a UIButton in storyboard

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

问题描述

我想通过源场景中存在的按钮来分割目标场景.我还想控制视图控制器之间的过渡动​​画(我想从右到左为 2 个视图设置动画).是否可以通过替换segue来做到这一点?我尝试了replace 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!

推荐答案

我发现 replace segue 和 push segue 具有误导性,因为 replace 似乎适用于 master detail 控制器,而 push segue 仅适用于导航控制器.在这种情况下,我需要实现自定义 segue.您需要继承 UIStoryboardSegue 并覆盖 perform 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.

这是我的代码的一个例子:

Here is an e.g of my code:

-(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-代码/

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

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

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