Re: Best way to model something that's drawable (i.e. has a visual representation in the UI)
Re: Best way to model something that's drawable (i.e. has a visual representation in the UI)
- Subject: Re: Best way to model something that's drawable (i.e. has a visual representation in the UI)
- From: Alastair Houghton <email@hidden>
- Date: Tue, 12 Jul 2016 19:26:21 +0100
On 12 Jul 2016, at 19:11, William Squires <email@hidden> wrote:
>
> Normally, of course, models shouldn't know anything about UI, and vice versa, but what about when the models themselves represent something visual that the UI needs to draw...
[snip]
> In this case, it makes sense that the object with the information needed to draw, is the model object itself…
[snip]
> So, in my app, the view's drawRect: method will [snip] ask for all the model objects that are visible within its bounds rect [snip] and will then iterate over them, asking them to draw themselves, now that the graphics context is properly set up.
This is the direct approach, and it’s a perfectly valid way to do things. The other option is to implement a Renderer object that uses the Visitor pattern (which, in a dynamic language like Objective-C, you might implement by dynamically generating the method name on the basis of the class name passed in; or in a static language like C++ or Swift, you might choose to do it by overloading a “draw” method). The direct way is usually faster; the Visitor approach has the advantage that you can easily implement different renderers without all the model objects needing to know what to do.
> The question is, if my GameObject (base class) defines a drawRect(bounds: CGRect) itself, and the code in the UIView subclass' drawRect: method passes a CGRect (which is within it's own bounds) to the model's drawRect:, will the code in the model's drawRect: "see" the graphics context of the UIView, so that you can set the stroke or fill color using the setStroke() and setFill() methods on UIColor, as well as anything else that may require that context, such as lower-level calls to CoreGraphics?
Yes. Cocoa keeps track of the current graphics context.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden