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/