0

I'm still learning some of the ins and outs of custom view drawing in Cocoa. I have a custom view where I draw lines and points based on the corresponding points in a larger rect elsewhere of a fixed size. I would like to have my drawing scale up or down when the view is resized, but maintain an aspect ratio same as the larger rect.

What is the best way to scale the drawing? Do I need to somehow apply an affine transform? Or should I be drawing to an imageRef? I'm not really sure exactly how to do ether one in this case or how to keep that in sync with the size of the view and the aspect ratio of the larger rect where coordinates come from. Any tips or links to example code are greatly appreciated.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55

1 Answers1

1

Concatenating an affine transform sounds like the right solution. Scaling by the same factor in both dimensions will preserve the aspect ratio of your drawing, and you can use simple division to compute the right factor (assuming you aren't just getting it from a slider or something).

If you haven't already, I highly recommend reading the Cocoa Drawing Guide and Quartz 2D Programming Guide. There's a lot of overlap, but the explanations are not copy-and-pasted, so if one guide's explanation of something doesn't make sense, look it up in the other one and try reading that version.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Thanks. I have been reading and rereading these. I know already that most of AppKit drawing is OOP wrapping around Quartz/CG drawing, but some of that is a bit terse.. :) – uchuugaka Jun 07 '13 at 05:54
  • I know how to get the desired aspect ratio number, btw. It is simply figuring out the best scaling & constraining approach. I have tried simply using the NSWindow method setContentAspectRatio: but that seems to do nothing... as my view is the only subview of the contentView, and is set to be the same size and origin as content view. – uchuugaka Jun 07 '13 at 05:56
  • and I see your answer in http://stackoverflow.com/questions/16593820/setaspectratio-doesnt-work-until-the-window-is-resized – uchuugaka Jun 07 '13 at 05:57
  • @uchuugaka: `setContentAspectRatio:` and `setAspectRatio:` constrain window resizing (by the user). It won't, say, snap the frame to that aspect ratio, partly because it doesn't know whether to grow the frame, shrink the frame, try to maintain the same area, or what—so it leaves that in your hands for programmatic resizing. It also has nothing to do with drawing. – Peter Hosey Jun 07 '13 at 06:07
  • Yeah I get that. And the part about nothing to do directly with drawing. I think the part that blew my mind was drawing to a NSBitMapRep and the hurdles with that. – uchuugaka Jun 07 '13 at 06:20
  • As my first foray into some parts of this it will take some fumbling. – uchuugaka Jun 07 '13 at 06:21