iOS Storyboards中的水平翻转方向

5
我在故事板中设置了两个视图控制器,每个视图控制器都有一个操作按钮。我建立了两个转场:

  1. 从第一个视图控制器到第二个视图控制器:Modal,水平翻转。
  2. 从第二个视图控制器到第一个视图控制器:Modal,水平翻转。

一切正常,但我想要的是来回翻转,现在它在两个视图控制器之间翻转的方向相同。

如何最简单地实现翻转-翻转回效果?

3个回答

6
我遇到了相同的问题,幸运地找到了一个解决方案。
我使用了自定义segue来翻转回去。下面是我的FlipLeftSegue代码:
@implementation FlipLeftSegue
- (void)perform
{
    UIViewController *src = (UIViewController *) self.sourceViewController;
    UIViewController *dst = (UIViewController *) self.destinationViewController;    

    [UIView beginAnimations:@"LeftFlip" context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:src.view.superview cache:YES];
    [UIView commitAnimations];

    [src presentViewController:dst animated:NO completion:nil];
}
@end

这种解决方案唯一的问题在于,当动画开始时viewDidAppear方法会立即被调用,而使用普通转场时,则是在动画之后调用该方法。

这是因为您正在破坏视图控制器层次结构。请使用UIViewcontroller transitionViewController: ... - doozMen

5

不要创建一个新的 ViewController,我建议在第二个 ViewController 上访问 presentingViewController 属性,然后调用 -dismissViewControllerAnimated:completion: 方法。像这样:

-(IBAction)someResponderToYourCancelButtonOrWhatever:(id)sender {
    // Do some stuff
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

1

你最好使用NSZombie答案中的这段代码:

- (void)swapChildVCFrom:(UIViewController *)from to:(UIViewController *)to options:(enum UIViewAnimationOptions)options
{
    [self addChildViewController:to];
    [from willMoveToParentViewController:nil];

    // Adjust the new child view controller's view's frame
    // For example here just set it to fill the parent view
    to.view.frame = self.view.bounds;

    [self transitionFromViewController:from
                      toViewController:to
                              duration:1.0
                               options:options
                            animations:nil
                            completion:^(BOOL b)
                            {
                                [to didMoveToParentViewController:self];
                                [from.view removeFromSuperview];
                                [from removeFromParentViewController];
                            }];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接