2011-12-22 49 views
5

的頂部和底部我在尋找一種方式來畫上一個表視圖淡入淡出效果(和大綱視圖,但我認爲這將是相同的),當滾動內容。下面是從Fantastical應用的例子:淡出效果在NSTableView的/ NSOutlineView

fade effect on top

另外一個類似的淡入的上QuickLook的窗口here的視頻。


爲了這個,我試着子類一的tableview的滾動視圖使用此代碼:

#define kFadeEffectHeight 15 

@implementation FadingScrollView 

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

    NSGradient* g = [[NSGradient alloc] initWithStartingColor: [NSColor blackColor] endingColor: [NSColor clearColor]]; 

    NSRect topRect = self.bounds; 
    topRect.origin.y = self.bounds.size.height - kFadeEffectHeight; 
    topRect.size.height = kFadeEffectHeight; 

    NSRect botRect = self.bounds; 
    botRect.size.height = kFadeEffectHeight; 

    [NSGraphicsContext saveGraphicsState]; 
    [[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeDestinationAtop]; 
    // Tried every compositing operation and none worked. Please specify wich one I should use if you do it this way 

    [g drawInRect: topRect angle: 90]; 
    [g drawInRect: botRect angle: 270]; 

    [NSGraphicsContext restoreGraphicsState]; 
} 

...但是這並沒有什麼褪色,可能是因爲這是實際的表視圖之前調用畫了。我對如何做到這一點:(

順便說一句,無論是tableview中,我想有這種效果的outlineview不知道是基於視圖,應用程序是隻10.7。

+0

可以爲您提供的效果,或者你鼓舞人心上的應用程序的iTunes的鏈接視頻? – viggio24 2011-12-22 12:48:07

+0

@ viggio24在應用商店中添加了Fantastical的鏈接,並在另一個示例中添加了視頻 – Alex 2011-12-22 13:33:52

回答

7

在Mac OS X(因爲你的問題被標記了),有幾個陷阱讓這很難,尤其是對於有彈性滾動的獅子,

我已經(就在今天)把我認爲是比工作更好的方法直接使用表格或大綱視圖:自定義的NSScrollView子類,它可以將兩個「淡入淡出視圖」平鋪在剪輯視圖頂部的正確位置。JLNFadingScrollView可以配置所需的淡入淡出高度和顏色,並且是自由/開源的Github上。請尊重許可並享受。 :-)

Examples of JLNFadingScrollView