如何在 Cocoa AppKit 应用程序中实现缩放/缩放

发布于 2024-10-31 13:53:19 字数 177 浏览 1 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-11-07 13:53:19

每个 NSView 都有一个边界和框架,框架是描述视图在其父视图边界内的位置的矩形。大多数视图的边界为零原点,并且大小与其帧大小相匹配,但情况并非必须如此。您可以更改视图边界和框架的关系,以缩放和平移自定义绘图和子视图。当您更改视图的边界时,它也会递归地影响后代视图的绘制。

更改视图边界的最直接方法是使用 -[NSView scaleUnitSquareToSize:]。在您的其中一个视图中,尝试调用 [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)],您应该看到其中所有内容的大小似乎都是双倍的。

这是一个例子。创建一个带有包含自定义视图和按钮的窗口的 XIB 文件。将自定义视图的自定义类设置为 MyView。将按钮的操作连接到视图的 doubleSize: 操作。构建并运行并点击按钮。每按一次,自定义视图中的红色方块的大小就会加倍。

/// MyView.h
@interface MyView : NSView {
}
- (IBAction)doubleSize:(id)sender;
@end

/// MyView.m
@implementation MyView
- (IBAction)doubleSize:(id)sender {
    [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)];
    /// Important, changing the scale doesn't invalidate the display
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect {
    NSSize squareSize = NSMakeSize(8, 8);
    NSRect square = NSMakeRect([self bounds].size.width / 2 - squareSize.width / 2, 
                               [self bounds].size.height / 2 - squareSize.height / 2,
                               squareSize.width,
                               squareSize.height);
    [[NSColor redColor] set];
    NSRectFill(square);
}
@end

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.

/// MyView.h
@interface MyView : NSView {
}
- (IBAction)doubleSize:(id)sender;
@end

/// MyView.m
@implementation MyView
- (IBAction)doubleSize:(id)sender {
    [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)];
    /// Important, changing the scale doesn't invalidate the display
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect {
    NSSize squareSize = NSMakeSize(8, 8);
    NSRect square = NSMakeRect([self bounds].size.width / 2 - squareSize.width / 2, 
                               [self bounds].size.height / 2 - squareSize.height / 2,
                               squareSize.width,
                               squareSize.height);
    [[NSColor redColor] set];
    NSRectFill(square);
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文