2011-06-24 33 views
2

我有一個自定義框,我已經是NSBox的子類。我重寫drawRect:方法,並在其中繪製一個梯度這樣(假設我已經有一個start & end顏色):NSScrollView搞砸NSGradient(腐敗)

-(void)drawRect:(NSRect)dirtyRect { 
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:start endingColor:end]; 
    [gradient drawInRect:dirtyRect angle:270]; 
    [gradient release]; 
} 

查閱此框被添加作爲用於NSCollectionView原型圖的子視圖。在視圖的原始狀態,它看起來像這樣:

enter image description here

和滾動視圖的視線並重新之後,它看起來像這樣:

enter image description here

爲什麼我的梯度像這樣被損壞了,我該如何解決它?謝謝!

回答

1

你的問題是你正在繪製dirtyFrame,而不是盒子的整個矩形。我不知道這是否是正確的,但試試這個:

-(void)drawRect:(NSRect)dirtyRect { 
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:start endingColor:end]; 
    [gradient drawInRect:[self bounds] angle:270]; 
    [gradient release]; 
} 
+0

您獲獎。只需要調用'[gradient gradientInRect:[self bounds] angle:270];'並且解決了問題。謝謝!我會在可用時接受它。 –

+0

那麼我想,我會將它更新爲'[self-bounds]'而不是'[self frame]'。 – icktoofay

+0

好的。我會問你同樣的問題,我問Bavarious,是否有一個更好的地方來繪製這個,而不是'drawRect:'? –

3

dirtyRect說法並不一定代表整個箱體。如果可可決定只有原始幀的一個子幀需要(重新)繪製,則dirtyRect僅表示該子幀。如果您爲整個幀繪製了漸變,然後(重新)爲子幀繪製相同的漸變,則很可能它們不匹配。

嘗試:

[gradient drawInRect:[self bounds] angle:270]; 

代替。

還有一點需要注意:它看起來像你的漸變對象可以被緩存而不是在-drawRect:內被創建/釋放。

+0

我在你發佈它的時候就明白了。謝謝! –