How should I use multiple UIViews to represent areas on screen?
I am building an ipad app that has two basic regions on the screen:
- A dock on the left hand side contains "things" that you can drag onto the right side
- A free-form canvas on the right hand side
Under the presumption that the dock and the canvas views are both added as subviews to the viewcontroller's view,开发者_如何学Python my question is around the mechanics of dragging the "thing" with my finger from the dock onto the canvas... who should own the view that represents the "thing"? Should it be the uiview that represents the dock (and then when it moves from the dock to the canvas I remove it from the dock and add it to the canvas)? Or should it belong the root view of the viewcontroller? Or some other pattern?
I'd be interested in peoples thoughts on the design patterns involved here.
Thanks a lot. BTW, I've considered using something like cocos2d for this, but for the moment, I'd like to stay with the standard UIKit.
In Objective-C, you use UIViewController
s to control views. The logic of the program resides in these controllers. The idea is to separate view and model (data) and have a controller managed the interaction. This is called the MVC ("Model-View-Controller") design pattern.
In your case, the controller controls the main view
and its subviews. So it is exactly there where you would implement the logic of dragging one object (also a UIView
) from one subview to another. In your words, the dragged object "belongs" to this controller.
Maybe you have another class that keeps track of the things on the canvas, your "data model". The controller would need that to read and write the changes to it. Maybe there is some custom drawing necessary for the views. The views would implement this behavior themselves. In this way, you also follow the principle of encapsulation that helps writing re-usable code.
精彩评论