Re: view content not tracking window size
Re: view content not tracking window size
- Subject: Re: view content not tracking window size
- From: N!K <email@hidden>
- Date: Mon, 29 Sep 2014 20:35:38 -0700
Success! Many thanks, Graham,
Your explanation enlightened me, and your example got me on the right track. I didn’t know that not-scaling the contents is normal; I had been looking for whatever was wrong and how to fix it. The thought of scaling in code did occur to me, but I had absolutely no idea how to start. Stumped, I sought help and you came through.
I also appreciate the inputs from the other responders, but Graham’s help was explicit. Thank you all.
A couple of comments, if I may:
Implementing if( scaleThePath ) stumped me. I couldn’t find a good size change criterion when I found that drawRect is called twice at the start, before doing any manual resizing. There was about about a 10% change in scaleX and scaleY from the first to the second. This was greater than the changes while resizing. It got too complicated, so I ignored this after many tries. I accepted simply doing the transform every time and found no harm. Perhaps a more demanding application would justify more work.
In this part of drawRect, [tfm concat]; was commented out because it was doubling the scaling. Without it, the path tracks the frame perfectly during resizing. (I still don’t know how to use it properly and haven’t found an example.)
// scale the content to track the frame
{
NSBezierPath *path;
NSRect bounds;
CGFloat scaleX,scaleY;
}
scaleX =NSWidth(bounds)/NSWidth([path bounds]);
scaleY =NSHeight(bounds)/NSHeight([path bounds]);
NSAffineTransform* tfm = [[NSAffineTransform alloc] init];
[tfm scaleXBy:scaleX yBy:scaleY];
NSBezierPath* temp = [tfm transformBezierPath:path];
// [tfm concat];
[temp stroke];
On Sep 25, 2014, at 9:22 PM, Graham Cox <email@hidden> wrote:
> 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@hidden> 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@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