7

I have an NSView in which the user can draw circles. These circles are stored as an array of NSBezierPaths, and in drawRect:, I loop through the array and call -stroke on each of the paths. How do I add a button to zoom in and out the NSView? Just change the bounds of the view?

Thanks.

jasonbogd
  • 2,451
  • 4
  • 31
  • 42

3 Answers3

9

Send your view a scaleUnitSquareToSize: message.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
9

You might also find this informative:

https://developer.apple.com/library/content/qa/qa1346/_index.html

The code in that document lets you add a "scale" property to a view.

jblixr
  • 1,345
  • 13
  • 34
NSResponder
  • 16,861
  • 7
  • 32
  • 46
0

The above answers didn't work for my scenario but led me to a solution.

The updated link to @Peter's answer was helpful: scaleUnitSquareToSize

I have found two soultions for zooming:

  1. Cropping the bounds manually
  2. Scalling the bounds with scaleUnitSquareToSize

I have created a small test project. Both solutions can be found on my GitHub repo : BoundsAndFramesCroppingAndScalling

To understand bounds vs frames read this SO article: difference-between-the-frame-and-the-bounds.

Swift scalling code:

let scaleSize = NSSize(width: 0.5, height: 0.5)
// 0.5 - Half the size
// 1.0 - No calling
// 2.0 - double the size , ... etc
myView?.scaleUnitSquare(to: scaleSize)
myView?.needsDisplay = true
Darkwonder
  • 1,149
  • 1
  • 13
  • 26