开发者

Zooming in an NSView

开发者 https://www.devze.com 2022-12-22 18:25 出处:网络
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 a

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 开发者_Python百科to zoom in and out the NSView? Just change the bounds of the view?

Thanks.


Send your view a scaleUnitSquareToSize: message.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消