【问题标题】:Custom iOS navigation controller animation : is this wrong?自定义 iOS 导航控制器动画:这是错的吗?
【发布时间】:2014-01-09 13:46:07
【问题描述】:

有人要求我在两个 UIViewControllers 之间添加完全自定义的过渡,并提出了这个解决方案。它有效,但我不知道是否有更好/更优雅的方式来做到这一点。

完全自定义,我的意思是涉及修改第一个视图控制器子视图(移动它们,不仅仅是淡入淡出或其他),能够改变持续时间等。

我想知道两件事:

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

因为几行代码胜过一千个单词,这里有一个非常简单的例子来理解这个想法:

// 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 中。

感谢您的意见。

【问题讨论】:

    标签: ios objective-c animation uinavigationcontroller pushviewcontroller


    【解决方案1】:

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

    使用方法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/

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 2015-08-21
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多