6

我用故事板构建了一个应用程序,带有一个初始视图控制器,连接到许多后续视图控制器,顺序连接,使用交叉溶解segue。这些工作一扫。一切正常。但是,我不想被迫使用基本的转场,而是想要一个自定义的转场,它将视图控制器内容在屏幕上滑动和滑出,就像对导航控制器的推动一样,但是,可以根据是否向左和向右移动一个是在应用程序中前进或后退。

我已将 segue 设置为自定义,并创建并保存了 MySegue.h 文件。但是,我不知道如何编写自定义 segue 代码,当我在视图控制器之间移动时,当另一个视图控制器来回滑动时,它会将一个视图控制器滑出屏幕。

任何人都可以为我提供编码(应该很容易!),通过打开和关闭屏幕,自定义 segue 从一个视图控制器移动到下一个并返回,所以我不必使用基本的交叉溶解或Xcode 4.2 中提供的标准翻转?我将不胜感激。

4

3 回答 3

4

我在这里遇到了同样的问题,但我使用滑动手势从一个视图控制器转到下一个。使用情节提要本身设置转场效果很好,但是当我需要滑动以“返回”到上一个视图控制器时,动画从右到左而不是从左到右。为了解决这个问题,我做了以下事情:

  1. 在根视图控制器中嵌入了一个导航控制器。
  2. 使用 segues 推送新的视图控制器。
  3. 当我想“返回”到前一个视图控制器时,我没有在情节提要中添加一个 segue 来推送前一个视图控制器。相反,popViewControllerAnimated每当发生向后滑动时,我都会调用导航控制器的方法。
于 2012-03-19T17:19:26.300 回答
2

To create a custom segue that slides view controllers first create a class that extends UIStoryboardSegue, then override the perform selector. I just made something like this and it should work for you too. Here's my code:

#define kAnimationDuration 0.5

#import "CustomSlideSegue.h"

@implementation CustomSlideSegue

- (void)perform
{
    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    [sourceViewController.view addSubview:destinationViewController.view];
    [destinationViewController.view setFrame:sourceViewController.view.window.frame];

    [destinationViewController.view setBounds:sourceViewController.view.bounds];
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;

    if ( !self.slideLeft ) {
        [UIView animateWithDuration:kAnimationDuration
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             [destinationViewController.view setCenter:CGPointMake(screenSize.height + screenSize.height/2, screenSize.height/2 - 138)];
                             [destinationViewController.view setCenter:CGPointMake(screenSize.width/2 + 127, screenSize.height/2 - 138)];
                         }
                         completion:^(BOOL finished){
                             [destinationViewController.view removeFromSuperview];
                             [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                         }];
    } else {
        [UIView animateWithDuration:kAnimationDuration
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             [destinationViewController.view setCenter:CGPointMake(-1*screenSize.height/2, screenSize.height/2 - 138)];
                             [destinationViewController.view setCenter:CGPointMake(screenSize.width/2 + 127, screenSize.height/2 - 138)];
                         }
                         completion:^(BOOL finished){
                             [destinationViewController.view removeFromSuperview];
                             [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                         }];
    }
}

Then when you want to perform the segue use this code:

   UIViewController *destination = [self.storyboard instantiateViewControllerWithIdentifier:@"some identifier"];
   CustomZoomSegue *segue = [[CustomZoomSegue alloc] initWithIdentifier:@"segue vc identifier" source:self destination:destination];
   [self prepareForSegue:segue sender:self];
   [segue perform];

The CGPointMake calls are custom to my code (I used landscape orientation) but you should be able to change it to fit your needs. If you need further clarification or have any question let me know and I will try to answer.

NOTE: I use storyboards with no segue lines between the view controllers.

于 2013-08-16T10:46:46.097 回答
0

为什么不只使用 a UINavigationController?这似乎正是它们的设计目的。

于 2012-01-04T16:18:00.350 回答