可可-嵌套NSViews和CALayers以按比例调整大小 [英] cocoa -- getting nested NSViews and CALayers to resize proportionally

查看:97
本文介绍了可可-嵌套NSViews和CALayers以按比例调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的NSWindow的contentView是一个NSView子类.它还有一些其他的NSView子类作为子视图.子视图是基于层的,而这些层又包含子层.一些子层还有其他子层.

My NSWindow's contentView is an NSView subclass. It has some other NSView subclasses as subviews. The subviews are layer-based, and those layers in turn contain sublayers. Some of the sublayers have further sub-sublayers.

当窗口调整大小时,我希望整个事情按比例调整大小.进行设置的正确方法是什么?

I want the whole thing to resize proportionally when the window is resized. What is the right way to set it up so that will happen?

谢谢

我根本没有使用Interface Builder.

I am not using Interface Builder at all.

推荐答案

这是我在调整父窗口大小时要使NSView的内容按比例缩放的方法.首先,在界面构建器中,我将NSView添加到窗口中,然后在AppDelegate中添加了对它的引用.我的恰好被称为scrollView.我从scrollView中删除了所有自动调整大小的行为.

Here's what I've done to get the contents of an NSView to scale proportionally as I resize the parent window. First, in interface builder, I added my NSView to the window, then added a reference to it in my AppDelegate. Mine happens to be called scrollView. I removed all of the auto-sizing behaviour from the scrollView.

然后,在我的AppDelegate中添加以下内容:

Then, in my AppDelegate I added this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // keep the aspect ratio constant so that the content looks good
    [window setContentAspectRatio:NSMakeSize(2, 1)];
    window.delegate = self;
}

- (void)windowDidResize:(NSNotification *)notification {
    // size the scrollView to fill the window, but keep its bounds constant
    NSRect rect = [[window contentView] frame];
    NSRect oldBounds = [scrollView bounds];
    [scrollView setFrame:rect];
    [scrollView setBounds:oldBounds];
}

这也将AppDelegate也变成了窗口委托.很好,因为我没有太多逻辑.通过在更改帧的同时保持边界不变,scrollView的内容将按比例缩小.

This turns the AppDelegate into the window delegate too. Fine since I've not got much logic in it. By keeping the bounds constant while changing the frame, the contents of scrollView will be scaled down smoothly.

这篇关于可可-嵌套NSViews和CALayers以按比例调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆