开发者

How should I use multiple UIViews to represent areas on screen?

开发者 https://www.devze.com 2023-04-09 14:36 出处:网络
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

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 UIViewControllers 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号