2

我被要求在两个UIViewController之间添加完全自定义的转换,并提出了此解决方案。它的工作原理,但我不知道是否有一个更好/更优雅的方式来做到这一点。定制iOS导航控制器动画:这是错误的吗?

通过全定制,我的意思是,涉及修改的第一个视图控制器子视图(移动它们,不仅褪色或其他),能够改变持续时间等

我想知道两件事情:

  • 这可以以任何方式分解吗?也许我可能忘记了一些问题?
  • 你觉得这很丑吗?如果是这样,是否有更好的解决方案?

由于几行代码比千言万语好,这里是一个很简单的例子来理解这个概念:

// Considering that self.mainView is a direct subview of self.view 
// with exact same bounds, and it contains all the subviews 

DCSomeViewController *nextViewController = [DCSomeViewController new]; 
UIView *nextView = nextViewController.view; 

// Add the next view in self.view, below self.mainView 
[self.view addSubview:newView]; 
[self.view sendSubviewToBack:newView]; 

[UIView animateWithDuration:.75f animations:^{ 
    // Do any animations on self.mainView, as nextView is behind, you can fade, send self.mainView to wherever you want, change its scale... 
    self.mainView.transform = CGAffineTransformMakeScale(0, 0); 

} completion:^(BOOL finished) { 
    // Push the nextViewController, without animation 
    [self.navigationController pushViewController:vc animated:NO]; 

    // Restore old 
    self.backgroundImageView.transform = CGAffineTransformIdentity; 
}]; 

我仔细检查过,我认为可能会得到几件事情错误:

  • 推后,视图层次不破,一切都看起来不错,self.view遭受任何变化
  • 坡平的情况下,nextView不是我ñself.view任何更多。

感谢您的意见。

回答

2

在ViewControllers之间进行转换有不同的方法。你的代码有效,但你可以实现更官方的方式。

使用梅索德transitionFromViewController:toViewController: 检查教程:http://www.objc.io/issue-1/containment-view-controller.html#transitions

或者你可以使用UIViewControllerContextTransitioning检查教程:http://www.objc.io/issue-5/view-controller-transitions.htmlhttp://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/