Re: Views, frames, and bounds
Re: Views, frames, and bounds
- Subject: Re: Views, frames, and bounds
- From: Graham Cox <email@hidden>
- Date: Mon, 20 Oct 2008 00:29:24 +1100
On 20 Oct 2008, at 12:08 am, DKJ wrote:
When I run the code I get a nice red rectangle in the window, just
where I expected it. But when I replace "bounds" with "frame", I get
nothing.
That's the first puzzle. Maybe if I can understand what's happening
here, the other puzzles will be resolved too!
I can only suggest you're really over-thinking this, because the
concept is really very simple.
The frame is where your view is within its parent. The bounds is your
local co-ordinate system.
Let's say you have a simple view that is 100 x 100 pixels. When the
view is asked to draw, it wants to draw in a rect 0,0 to 100,100. If
that view is shifted within its window, you still want to be able to
draw from 0,0 to 100,100. This is your bounds. In other words, moving
the view doesn't change this, it always starts at 0,0.
The frame is where the view is in the parent - for simplicity let's
say that's the window. The size is still the same, 100 x 100, but the
origin will change depending on where the view is. It might be from
50,50 to 150,150 for example. It's analogous to a window on screen -
the window's local co-ordinates always start at 0,0 but the window can
be dragged anywhere on the screen and the window content doesn't need
to take that into account. Views take the same idea one step further,
so you can draw a view's content without caring about its position.
Note - when drawing the content of a view, you very rarely need to
know what the frame is. Only the bounds tends to matter. Also, the co-
ordinate system is set up to be the bounds when the call to drawRect:
is made.
If you substitute [self frame] for [self bounds] when drawing, it may
or may not produce visible results depending on where your view is
positioned. Since you are always drawing relative to the bounds, using
the frame here is incorrect. For example if your frame's origin is
50,50, drawing the frame rect will start at 50,50 relative to your
bounds' origin. That could well be beyond the edge of the bounds and
so you might see nothing.
In the simple case, the SIZE of the frame and the bounds is the same,
but the position is different - the bounds will always have an origin
of 0,0 no matter where the view is positioned.
However, the size of the frame and the bounds can also be different.
This occurs when you have a scrollable view (where the bounds is
larger than the frame, and so can be scrolled around) and also when
there is a scaling factor (zoom).
hth,
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