How to implement zoom/scale in a Cocoa AppKit-application

后端 未结 1 1078
离开以前
离开以前 2021-02-09 03:04

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

1条回答
  •  独厮守ぢ
    2021-02-09 03:40

    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
    

    0 讨论(0)
提交回复
热议问题