理解UIViewController转换的值

时间:2021-10-25 15:01:09

I am trying to learn some new iOS programming patterns. I have read a bunch about the UIViewController transitioning APIs added in iOS 7. They look cool but also feel pretty heavy for what seems like a simpler task.

我正在尝试学习一些新的iOS编程模式。我已经阅读了一些关于iOS 7中添加的UIViewController转换API的内容。它们看起来很酷但是对于看起来更简单的任务也感觉相当沉重。

Consider this use case: I have a custom container view controller that manages "slides". It holds an array of slide view controllers and the user can move forward and backward though them by tapping a button.

考虑这个用例:我有一个管理“幻灯片”的自定义容器视图控制器。它拥有一系列幻灯片视图控制器,用户可以通过点击按钮向前和向后移动。

I can implement the transition for this as follows:

我可以按如下方式实现此过渡:

private func transitionToViewController(viewController: UIViewController, direction: TransitionDirection = .Forward, animated: Bool = true) {
    currentViewController.willMove(toParentViewController: nil)
    addChildViewController(viewController)
    // ... set up frames, other animation prep ...
    contentContainerView.addSubview(comingView)
    UIView.animate(duration: 0.5, animations: { 
        // do the animations
    }) { (finished) in
        leavingView.removeFromSuperview()
        self.currentViewController.removeFromParentViewController()
        viewController.didMove(toParentViewController: self)
        // final clean up
    } 
}

How would the newer transitioning APIs improve this? From what I understand, these APIs are even more complicated to use if you are rolling your own container view controllers (see custom-container view controller transitions.

新的转换API将如何改善这一点?据我所知,如果您正在滚动自己的容器视图控制器,这些API使用起来会更加复杂(请参阅自定义容器视图控制器转换。

Is the value in the transitioning APIs mostly for interactive transitions?

转换API中的值主要用于交互式转换吗?

Thanks for clarifying

谢谢你的澄清

2 个解决方案

#1


7  

I think the new transitioning API (UIViewControllerTransitioningDelegate and friends) is simply a final step in the generalization of view transitions between controllers.

我认为新的转换API(UIViewControllerTransitioningDelegate和朋友)只是控制器之间视图转换概括的最后一步。

In first versions of UIKit we had to hack the system transition code to get any custom transitions at all. Years later we got controller containment that made it possible to manage view controllers as first-class citizens and create our own interactive transitions. The final step is having a full-featured general system API for any transition that you can dream of – that’s the new transition API.

在UIKit的第一个版本中,我们不得不破解系统转换代码以获得任何自定义转换。多年以后,我们获得了控制器控制功能,可以将视图控制器作为一等公民进行管理,并创建自己的交互式转换。最后一步是为您可以实现的任何转换提供功能齐全的通用系统API - 这是新的转换API。

The new API makes it possible to extract the transitions into standalone classes. Which, in turn, makes it finally possible to just download a transition library off GitHub and plug it into your existing code as a simple transition delegate. No need to derive your view controllers from some particular superclass, no need to use a third-party controller container, no need to add extensions to UIKit classes. Now the transitions are finally first-class citizens in UIKit, too.

新的API可以将转换提取到独立的类中。反过来,这使得最终可以从GitHub下载转换库并将其作为简单的转换委托插入到现有代码中。无需从某个特定的超类派生您的视图控制器,无需使用第三方控制器容器,无需向UIKit类添加扩展。现在,转型终于成为了UIKit的一流公民。

#2


2  

How would the newer transitioning APIs improve this?

新的转换API将如何改善这一点?

TL;DR Maintainability via Encapsulation, Reusability, Testability.

TL; DR通过封装,可重用性,可测试性的可维护性。

Encapsulation: The comments indicate that there's logic to set up and state to track with the animation. Your view controller is probably plenty big enough already; putting transition logic somewhere else makes each piece smaller and therefore more maintainable.

封装:注释表明有动态设置和状态跟踪的逻辑。您的视图控制器可能已经足够大了;将转换逻辑放在其他地方会使每个部分变得更小,因此更易于维护。

Reusability: What's the next thing you'll do after this? Set up the transition back from the transitioned to controller, no doubt. And is it likely to be a reversal of this animation? Pretty likely. So you'll copy and paste this code to that controller and reverse it there, probably. Now you've got two copies. One copy using the transitions would be more maintainable. (Also note the existence of pods of custom transitions, as reusability and shareability go hand in hand.)

可重用性:接下来你会做什么呢?毫无疑问,设置从过渡到控制器的过渡。它可能是这个动画的逆转吗?很有可能。因此,您可以将此代码复制并粘贴到该控制器,并在那里将其反转。现在你有两份副本。使用转换的一个副本将更易于维护。 (还要注意自定义过渡的pod的存在,因为可重用性和可共享性是相辅相成的。)

Testability: Code embedded in a heavyweight view controller is notoriously difficult to test. A custom transition can be tested in isolation without the state overhead of the live views.

可测试性:嵌入在重量级视图控制器中的代码众所周知难以测试。可以单独测试自定义转换,而无需实时视图的状态开销。

So for any code you intend to look at more than once, the transitioning APIs are probably worth the effort!

因此,对于您打算不止一次查看的任何代码,转换API可能是值得的!

#1


7  

I think the new transitioning API (UIViewControllerTransitioningDelegate and friends) is simply a final step in the generalization of view transitions between controllers.

我认为新的转换API(UIViewControllerTransitioningDelegate和朋友)只是控制器之间视图转换概括的最后一步。

In first versions of UIKit we had to hack the system transition code to get any custom transitions at all. Years later we got controller containment that made it possible to manage view controllers as first-class citizens and create our own interactive transitions. The final step is having a full-featured general system API for any transition that you can dream of – that’s the new transition API.

在UIKit的第一个版本中,我们不得不破解系统转换代码以获得任何自定义转换。多年以后,我们获得了控制器控制功能,可以将视图控制器作为一等公民进行管理,并创建自己的交互式转换。最后一步是为您可以实现的任何转换提供功能齐全的通用系统API - 这是新的转换API。

The new API makes it possible to extract the transitions into standalone classes. Which, in turn, makes it finally possible to just download a transition library off GitHub and plug it into your existing code as a simple transition delegate. No need to derive your view controllers from some particular superclass, no need to use a third-party controller container, no need to add extensions to UIKit classes. Now the transitions are finally first-class citizens in UIKit, too.

新的API可以将转换提取到独立的类中。反过来,这使得最终可以从GitHub下载转换库并将其作为简单的转换委托插入到现有代码中。无需从某个特定的超类派生您的视图控制器,无需使用第三方控制器容器,无需向UIKit类添加扩展。现在,转型终于成为了UIKit的一流公民。

#2


2  

How would the newer transitioning APIs improve this?

新的转换API将如何改善这一点?

TL;DR Maintainability via Encapsulation, Reusability, Testability.

TL; DR通过封装,可重用性,可测试性的可维护性。

Encapsulation: The comments indicate that there's logic to set up and state to track with the animation. Your view controller is probably plenty big enough already; putting transition logic somewhere else makes each piece smaller and therefore more maintainable.

封装:注释表明有动态设置和状态跟踪的逻辑。您的视图控制器可能已经足够大了;将转换逻辑放在其他地方会使每个部分变得更小,因此更易于维护。

Reusability: What's the next thing you'll do after this? Set up the transition back from the transitioned to controller, no doubt. And is it likely to be a reversal of this animation? Pretty likely. So you'll copy and paste this code to that controller and reverse it there, probably. Now you've got two copies. One copy using the transitions would be more maintainable. (Also note the existence of pods of custom transitions, as reusability and shareability go hand in hand.)

可重用性:接下来你会做什么呢?毫无疑问,设置从过渡到控制器的过渡。它可能是这个动画的逆转吗?很有可能。因此,您可以将此代码复制并粘贴到该控制器,并在那里将其反转。现在你有两份副本。使用转换的一个副本将更易于维护。 (还要注意自定义过渡的pod的存在,因为可重用性和可共享性是相辅相成的。)

Testability: Code embedded in a heavyweight view controller is notoriously difficult to test. A custom transition can be tested in isolation without the state overhead of the live views.

可测试性:嵌入在重量级视图控制器中的代码众所周知难以测试。可以单独测试自定义转换,而无需实时视图的状态开销。

So for any code you intend to look at more than once, the transitioning APIs are probably worth the effort!

因此,对于您打算不止一次查看的任何代码,转换API可能是值得的!