2011-02-28 18 views
1

在試圖讓一個可拖動排序的CALayer(Question on Stack Overflow here)我試圖通過創建代碼的窗口和添加的CALayer給它的,但我不明白爲什麼它不顯示。的CALayer沒有顯示

NSRect rect = NSZeroRect; 
    rect.size = NSMakeSize(SSRandomFloatBetween(300.0, 200.0), SSRandomFloatBetween(300.0, 200.0)); 

    NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSWindowBackingLocationDefault defer:YES]; 
    [newWin setBackgroundColor: [NSColor clearColor]]; 
    [newWin setOpaque:NO]; 
    [newWin setIgnoresMouseEvents:NO]; 
    [newWin setMovableByWindowBackground:YES]; 
    [newWin makeKeyAndOrderFront:self]; 

    [[newWin contentView] setWantsLayer:YES]; 

    NSRect contentFrame = [[newWin contentView] frame]; 
    CALayer *newWinLayer = [CALayer layer]; 
    newWinLayer.frame = NSRectToCGRect(contentFrame); 

    layer.backgroundColor=CGColorCreateGenericGray(0.0f, 0.5f); 
    layer.borderColor=CGColorCreateGenericGray(0.756f, 0.5f); 
    layer.borderWidth=5.0; 

     // Calculate random origin point 
    rect.origin = SSRandomPointForSizeWithinRect(rect.size, [window frame]); 

     // Set the layer frame to our random rectangle. 
    layer.frame = NSRectToCGRect(rect); 
    layer.cornerRadius = 25.0f; 
    [newWinLayer addSublayer:layer]; 

「窗口」鏈接到一個大窗口,半透明(黑色填充)窗口被調整大小以填滿屏幕。

我所做的窗口拖動的,但爲什麼不是CALayer的窗口中顯示?

回答

2
NSRect rect = NSZeroRect; 

rect.size = NSMakeSize(SSRandomFloatBetween(300.0, 200.0), 
     SSRandomFloatBetween(300.0, 200.0)); 

NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect 
      styleMask:NSBorderlessWindowMask 
      backing:NSWindowBackingLocationDefault defer:YES]; 

[newWin setBackgroundColor:[NSColor clearColor]]; 
[newWin setOpaque:NO]; 
[newWin setIgnoresMouseEvents:NO]; 
[newWin setMovableByWindowBackground:YES]; 
[newWin makeKeyAndOrderFront:self]; 

// you don't want to do this yet 
// [[newWin contentView] setWantsLayer:YES]; 

NSRect contentFrame = [[newWin contentView] frame]; 
CALayer *newWinLayer = [CALayer layer]; 
newWinLayer.frame = NSRectToCGRect(contentFrame); 

線斷裂,也有記憶同治問題:

// NOTE: remember that the following 2 *Create* methods return 
// results that need to be released, unless you're using Garbage-Collection 
// Also, I'm guessing that `layer` is created somewhere? 
CALayer *layer = [CALayer layer]; 
CGColorRef backgroundCol = CGColorCreateGenericGray(0.0f, 0.5f); 
CGColorRef borderCol = CGColorCreateGenericGray(0.756f, 0.5f); 

layer.backgroundColor=backgroundCol; 
layer.borderColor=borderCol; 
CGColorRelease(backgroundCol); CGColorRelease(borderCol); 
layer.borderWidth=5.0; 

    // Calculate random origin point 
rect.origin = SSRandomPointForSizeWithinRect(rect.size, [window frame]); 

    // Set the layer frame to our random rectangle. 
layer.frame = NSRectToCGRect(rect); 
layer.cornerRadius = 25.0f; 

[newWinLayer addSublayer:layer]; 

NSView *view = [newWin contentView]; 

// the order of the following 2 methods is critical: 

[view setLayer:newWinLayer]; 
[view setWantsLayer:YES]; 

見的NSView對setWantsLayer:

討論 順序說明文件 setWantsLayer:setLayer:被 稱爲是重要的是,它使得 層之間的區別 視圖和圖層託管視圖。

阿層支持的觀點是一種觀點認爲是 由核心動畫層的支持。由視圖完成的任何 繪圖都是緩衝層 。通過簡單地調用 setWantsLayer:,您配置了一個 圖層支持的視圖,其值爲YES。 視圖類會自動 爲您創建一個背襯層, 並使用視圖類的繪圖 機制。 當使用層支持 意見你永遠不應該直接與層互動 。

圖層託管視圖是 包含您打算直接操作的核心動畫圖層的視圖。 通過 實例化Core層的實例並使用視圖的setLayer: 方法設置該層的 層的實例。這樣做後,您 調用setWantsLayer:值爲 是的。 當使用圖層託管視圖 時,不應該依靠 圖形的視圖,也不應將子視圖 添加到圖層託管視圖

我相信你這樣做的方式會試圖創建一個層次支持的視圖,在那裏你不應該像你一樣試圖與視圖的底層進行交互。你想要的是圖層託管品種。