When the view resizes, the content area changes to become larger or smaller,
but the scaling of content within that view does not change. You have to
arrange this if that's what you want.
There are a number of ways to do this, but nothing automatic - no amount of
fiddling with constraints will achieve this. You can either scale the view,or
you can scale the path that you draw inside the view.
The latter is probably easiest - using the view's bounds, you can compute a
transform that will scale the path to fit that bounds, then use -
transformBezierPath: to create a new temporary path the right size. If your
original path is calculated based on a "unit square", that is a bounds rect of
size 1.0, 1.0, then calculating the scale factor is trivially easy, though any
rect isn't much harder.
Bear in mind that this approach scales the path, but not the stroke width, etc
used to draw it. If you scale the view instead, then any stroke widths are also
scaled and in that case you don't need to create temporary paths, but you do
need to compute the same transform and concat it with the current CTM when you
draw the path.
Off the top of my head (not tested, typed in Mail), the sort of code you need
is:
- (void) drawRect:(NSRect) dirtyRect
{
NSRect br = [self bounds];
CGFloat scaleX, scaleY;
scaleX = NSWidth([path bounds]) / NSWidth( br );
scaleY = NSHeight([path bounds]) / NSHeight( br );
NSAffineTransform* tfm = [NSAffineTransform alloc] init];
[tfm scaleXBy:scaleX yBy:scaleY];
if( scaleThePath )
{
NSBezierPath* temp = [tfm transformBezierPath:path];
[self drawMyPath:temp];
}
else
{
[tfm concat];
[self drawMyPath:path];
}
}
--Graham
On 26 Sep 2014, at 12:38 pm, N!K <[email protected]> wrote:
> In Xcode 5 OSX, not ios, I have created a custom view and set auto layout
> constraints so that the custom view's sides stay a fixed distance from the
> content view's frame. The custom view resizes correctly while dragging the
> window's corner while running, but the content of the custom view remains
> fixed in size. Shrinking the window can crop the content, and expanding it
> provides lots of open space next to the unchanging content.
>
> The content consists of a Bezier path, which is created in initwithframe and
> executed in drawrect with [path stroke]. NSLog shows that bounds is changing
> while resizing.
>
> How can I make the content resize along with the view and window? The window,
> view, and drawing documents explain how to set up a view, but I haven't found
> any discussion of content tracking the window size.
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]