Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 895
finnsson Avatar asked Apr 09 '11 08:04

finnsson


1 Answers

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
like image 177
Jon Hess Avatar answered Nov 12 '22 13:11

Jon Hess