Re: Custom view resizing
Re: Custom view resizing
- Subject: Re: Custom view resizing
- From: Robert Olivier <email@hidden>
- Date: Mon, 01 Jun 2009 13:39:42 -0700
Thanks Graham,
While I was waiting for my post to get moderated I did implement the
data model method of calculating the view bounds and thats working
great.
rjo
On May 29, 2009, at 11:55 PM, Graham Cox wrote:
On 29/05/2009, at 10:14 AM, Robert Olivier wrote:
I'm a new Cocoa programmer working on my first non-trivial custom
view and I have a question regarding the right way to handle view
resizing.
Say that I have a list of shapes that I need to draw left to right
across my view, and this list grows over time. Eventually, I will
need to draw a shape outside of the existing view frame and I when
this happens I want to resize the view.
Initially I had all of the drawing code directly in the view and
when the code detected that the next shape would be outside of the
existing view bounds it would resize it like this:
if((current + spacing + headerSize.width) >= rect.size.width) {
NSSize newSize;
newSize.width = [self frame].size.width + spacing+headerSize.width;
newSize.height = [self frame].size.height;
[super setFrameSize:newSize];
[self setNeedsDisplay:YES];
}
This is a very bad idea. From what you're saying this is called
within -drawRect: You should never try do do anything except drawing
there.
I want to move to a model where the shapes draw themselves rather
than having all of the drawing code in the custom view, but without
the shape object knowing anything about the NSView object itself,
only the NSRect that is being drawn.
What is the best practice for this? Am I thinking right that the
shapes should draw themselves rather inspecting each shape and
drawing it in the view?
There are differing views on this, but yes, I think this is a good
way to go. I'd suggest you do need a way to tell the shapes what
view they are drawing in though, so they can ask it whether they
really need to draw (using -needsToDrawRect:, etc).
IF so, and a shape object knows that the view is not big enough for
it to render itself, should it send a notification to the view
telling it to resize or would it be best to just let the shape
objects know about the NSView object? What are the implications
for pagination during a print job?
Do NOT attempt to resize the view while drawing. It doesn't matter
if this is triggered by a notification or in some other way,
resizing the view at drawing time is a bad idea. Don't do it.
Instead, look at it another way. You are adding and removing shapes
in a data model. Each shape has a bounding rect. The data model
could calculate the union of the bounding rect easily, so when a
shape is added/removed, ask the DM for the overall bounds and size
the view to that. This is done totally outside of the drawing code.
Adding/Removing/Resizing will trigger drawing, which then just
iterates over the shapes and asks them to draw. At that point the
view will be the right size.
--Graham
_______________________________________________
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