为什么我的CALayer被我的NSView剪辑?

时间:2013-10-09 18:30:55

标签: macos cocoa core-animation calayer

Core Animation Programming Guide让我相信,默认情况下,子层可以扩展到主机视图的边界之外而不会被剪切。但这不是我发生的事情。

以下是我初始化图层托管自定义视图的方法:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil) {
        mBaseImage = [[NSImage imageNamed: @"Button.png"] retain];
        mSliderImage = [[NSImage imageNamed: @"Track.png"] retain];

        mRootLayer = [CALayer layer];
        mRootLayer.masksToBounds = NO;
        [self setLayer: mRootLayer ];
        [self setWantsLayer: YES];
        mRootLayer.contents = mBaseImage;

        mSliderLayer = [CALayer layer];
        [mRootLayer addSublayer: mSliderLayer];
        NSSize imSize = [mSliderImage size];
        NSRect sliderBounds = NSMakeRect( 0.0f, 0.0f, imSize.width, imSize.height );
        mSliderLayer.bounds = sliderBounds;
        mSliderLayer.contents = mSliderImage;
        mSliderLayer.position = NSMakePoint( 31.0f, 31.0f );
    }

    return self;
}

主机视图和用作其内容的图像为62x62,而子图层图像为90x10。但整个事情被限制在62x62的范围内。我错过了什么?

1 个答案:

答案 0 :(得分:5)

诀窍似乎是视图必须包含在具有图层的另一个视图(可能是窗口的内容视图)中。 (有记录吗?)