Class architecture for implementing a fully custom containerViewController in iOS

江枫思渺然 提交于 2019-12-12 15:52:05

问题


I'm currently trying to build my own Custom ContainerViewController.

I'm quite familiar with the iOS ViewController containment API (introduced in iOS 5) and the new iOS7 ViewController Transition API.

While implementing my container, i tried to utilize the same patterns UINavigationController and UITabBarController are using.

My Container is working well so far and using animated and interactive transitions properly.

The Problem is, i packed a huge amount of the logic into my UIViewController container subclass. It is conforming to <UIViewControllerContextTransitioning> and uses iVars to store all the values returned by that protocol's methods.

The animation and interaction logic is already separated in another class and third-parties are also able to provide their own transition using a delegate similar to UINavigationControllerDelegate and UITabBarControllerDelegate.

What I'm trying to do now is out-sourcing the UIViewControllerContextTransitioning to a separate class to create the same modularity Apple does for it's containerVCs. Apple provides a UIViewControllerOneToOneTransitionContext (private API) for the id<UIViewControllerContextTransitioning> object handed to the UIViewControllerAnimatedTransitioning and UIViewControllerInteractiveTransitioning methods. So they are NOT using their UIViewController subclass for that. (That's what i do at the moment)

My current structure is in so far tempting to keep for me as when the transition logic calls [updateInteractiveTransition:], [completeTransition:], etc. on the context, these calls are made directly on my containerController which can then respond by updating other elements in it's view. (Like UINavigationController does when you call [updateInteractiveTransition:] and it's updating the content of the NavigationBar (cross-fade)).

Out-Sourcing the contexttransitioning logic in another class would mean:

  • Provide the viewControllers and frames, etc. which are hold in the stack of my container FROM the container TO the context object because the context needs to provide them to the transition logic.
  • Call transition-callbacks FROM the context object ON the container because the context object is the one receiving them from the transition logic.

As Apple uses this class-relationship, there must be some advantage about it, i guess. Currently, i don't know if i should keep my implementation, or try to make it as modular as Apple-provided containers.

Also see this SO - Question where i asked the same thing. (More like an answer-question, sorry for that :/ )

While we're on the topic : Is it possible to make my container work with UIPercentDrivenInteractiveTransition ? The documentation says it cuts the animation executed by the transitionAnimator in keyframes and automagically "replays" the animated transition step by step, so i doubt it can be used with custom containers.

Thanks for your help !


回答1:


Apple uses a lot of small classes conforming to certain protocols to hand over pieces of work. Divide and conquer. Have a look at for instance the way UITableView operates or collection view. They did split things into smallest chunks possible, and provided each one with some generic objects. These objects only conform to certain Protocols.

  • Do not force people to subclass.
  • Create protocols that classes fulfilling certain roles have to conform to.
  • Where you want to create ready to use objects that will perform certain actions - return id type of object, not a class object. That's the only way to keep things simple and flexible enough. Apple does that even with NSObject, which is both a class and protocol. When creating your own protocols, remember to make them conform to NSObject * protocol.

Your question is rather long, and does not ask any specific questions, so I hope this answers some of your concerns. If not, feel free to post one to response to this.



来源:https://stackoverflow.com/questions/21804111/class-architecture-for-implementing-a-fully-custom-containerviewcontroller-in-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!