2012-03-08 37 views

回答

0

您需要中的插入文檔視圖中的內容,以便用於陰影來顯示,然後層背面的圖,並設置了一片陰影它。例如:

view.wantsLayer = YES; 
NSShadow *shadow = [NSShadow new]; 
shadow.shadowColor = [NSColor blackColor] 
shadow.shadowBlurRadius = 4.f; 
shadow.shadowOffset = NSMakeSize(0.f, -5.f); 
view.shadow = shadow; 
0

的NSScrollView內容查看是一個的NSView子類,其中有一個影子字段,如果創建一個陰影對象並將其分配給該字段,該視圖將自動地縮回

NSShadow* shadow = [[NSShadow alloc] init]; 
    shadow.shadowBlurRadius = 2; //set how many pixels the shadow has 
    shadow.shadowOffset = NSMakeSize(2, -2); //the distance from the view the shadow is dropped 
    shadow.shadowColor = [NSColor blackColor]; 
    self.scrollView.contentView.shadow = shadow; 
當顯示陰影

這是可行的,因爲所有視圖在drawRect上繪製時使用[shadow set]使用此陰影屬性。

平局操作過程中做[影子集]讓無論是繪製後,爲下

+0

你會考慮增加一些敘述來解釋爲什麼這段代碼有效嗎?是什麼使它成爲問題的答案?這對詢問問題的人以及任何其他人來說非常有幫助。 – 2013-03-11 18:00:10

0

我是新來的堆棧溢出進入帖子被複制,但我有同樣的問題,並解決它,所以我想在網上搜索幾個小時以找到解決方案後,回答它會很好。

我的解決方案是創建一個NSClipView子與drawRect中下面的代碼...

- (void)drawRect:(NSRect)dirtyRect 
{ 
    [super drawRect:dirtyRect]; 

    NSRect childRect = [[self documentView] frame]; 

    [NSGraphicsContext saveGraphicsState]; 

    // Create the shadow below and to the right of the shape. 
    NSShadow* theShadow = [[NSShadow alloc] init]; 
    [theShadow setShadowOffset:NSMakeSize(4.0, -4.0)]; 
    [theShadow setShadowBlurRadius:3.0]; 

    // Use a partially transparent color for shapes that overlap. 
    [theShadow setShadowColor:[[NSColor grayColor] 
          colorWithAlphaComponent:0.95f]]; 

    [theShadow set]; 

    [[self backgroundColor] setFill]; 

    NSRectFill(childRect); 

    // Draw your custom content here. Anything you draw 
    // automatically has the shadow effect applied to it. 

    [NSGraphicsContext restoreGraphicsState]; 
} 

然後你需要創建一個子類的實例,並與選擇的setContentView設置。

您還需要在每次內容視圖大小更改時重新繪製剪輯視圖。如果您的內容視圖設置爲根據用戶需要的畫布大小進行更改,則除非您重新繪製剪輯視圖,否則會留下一些令人厭惡的陰影標記。

你不需要像其他人所建議的那樣混淆剪輯。

希望它有幫助!