如何在 Cocoa AppKit 应用程序中实现缩放/缩放
如何在 Cocoa AppKit 应用程序中实现缩放/缩放(即不最大化窗口,而是缩放窗口及其所有子视图)?我认为它在 iOS 中称为“zoomScale”。可以使用 Core Animations 或 Quartz 2D (例如 CGContextScaleCTM )来完成吗?还是我必须在所有 NSView、NSCell 等中手动实现它?
How do you implement zoom/scale in a Cocoa AppKit-application (i.e. not maximizing the window but scaling the window and all its subviews)? I think it's called zoomScale in iOS. Can it be done using Core Animations or Quartz 2D (e.g. CGContextScaleCTM
) or am I forced to implement it manually in all my NSViews, NSCells, etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 NSView 都有一个边界和框架,框架是描述视图在其父视图边界内的位置的矩形。大多数视图的边界为零原点,并且大小与其帧大小相匹配,但情况并非必须如此。您可以更改视图边界和框架的关系,以缩放和平移自定义绘图和子视图。当您更改视图的边界时,它也会递归地影响后代视图的绘制。
更改视图边界的最直接方法是使用 -[NSView scaleUnitSquareToSize:]。在您的其中一个视图中,尝试调用 [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)],您应该看到其中所有内容的大小似乎都是双倍的。
这是一个例子。创建一个带有包含自定义视图和按钮的窗口的 XIB 文件。将自定义视图的自定义类设置为 MyView。将按钮的操作连接到视图的 doubleSize: 操作。构建并运行并点击按钮。每按一次,自定义视图中的红色方块的大小就会加倍。
Each NSView has a bounds and frame, the frame is the rectangle that describes a view's placement within its superview's bounds. Most views have a bounds with a zero origin, and a size that matches their frame size, but this doesn't have to be the case. You can change the relationship of a view's bounds and frame to scale and translate both custom drawing and subviews. When you change the bounds of a view, it also affects the drawing of descendant views recursively.
The most straightforward way to change the bounds of a view is with -[NSView scaleUnitSquareToSize:]. In one of your views, try calling [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)], and you should see the size of everything inside of it appear to be double.
Here's an example. Create a XIB file with a window containing a custom view, and a button. Set the custom view's custom class to MyView. Connect the button's action to the view's doubleSize: action. Build and run and hit the button. The red square in the custom view should double in size with each press.