Re: drawRect runs twice, bounds are changed in between
Re: drawRect runs twice, bounds are changed in between
- Subject: Re: drawRect runs twice, bounds are changed in between
- From: Graham Cox <email@hidden>
- Date: Fri, 16 Jan 2015 07:55:05 +1100
> On 15 Jan 2015, at 3:39 pm, N!K <email@hidden> wrote:
>
> I’m trying to learn more about drawing. One stumbling block was getting an NSBezierPath to change size proportional to the window size when the corner is dragged. Per Graham Cox’s suggestion, the view size change can be detected in subsequent passes of drawRect by comparing
> ratioX = NSWidth ([_path bounds])/NSWidth(bounds);
> ratioY = NSHeight([_path bounds])/NSHeight(bounds);
> with their initial values, which were established in the one pass of drawRect before the view appeared.
>
This wasn't my suggestion, exactly.
Don't use -drawRect to "establish" anything - just use it to draw. Only draw. Only ever draw.
My actual suggestion was to create your bezier path at some known size - I've used a size of 1 x 1 centred at 0,0 for example, then scale it to your needs *WHEN* you draw it, then discard the scaled version. That way you are proof against whatever size your view is, and when and how often it's called. You don't (and shouldn't) need a -resize method because you never store the results of it anyway.
All that said, this is just toy code that shows you how you can apply scaling to a path - it isn't necessarily much use in real-world app. One problem with it for example is that it's possible to end up (if your view is zero-sized, as it can be when initialized) with div-by-zero errors that end up putting NaNs into the path's coordinates. Core Graphics (which has always tolerated that sort of thing historically) will, since 10.9, throw a fit and summarily abort your app.
If you want to allow your view to zoom, rather than just display some content scaled, look into the relationship between the bounds and the frame of the view. Setting these different will cause zooming. If you want a slghtly more realistic way to place bezier path objects in a view, one approach could be to create an object that has a bezier path (maybe at a fixed size, such as 1 x 1), and also other states of it, such as its drawn size, angle and position from which you can create a transform. It's also convenient to include things such as stroke and fill colours.
--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