在10.7+項目中,當鼠標位於集合視圖項目視圖的界限內時,我試圖在NSCollectionViewItem中啓用UI元素。對於該填充NSCollectionView每個集合視圖項目,我一直在使用它的界限它創建一個單獨的NSTrackingArea自定義視圖:直到NSCollectionView內容滾動鼠標在NSCollectionView + NSScrollView
- (void) initializeMouseTracking
{
self.trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways owner:self userInfo:nil];
[self addTrackingArea:self.trackingArea];
NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
mouseLocation = [self convertPoint: mouseLocation fromView: nil];
if (NSPointInRect(mouseLocation, [self bounds]) == YES)
{
[self mouseEntered:nil];
}
else
{
[self mouseExited:nil];
}
}
跟蹤區域工作的偉大。顯然,跟蹤區域需要在滾動期間和滾動後重置。 我試圖無效並重新創建NSTrackingAreas在幾個方面:
- (void) resetMouseTracking
{
[self removeTrackingArea:self.trackingArea];
[self initializeMouseTracking];
}
- (void) scrollWheel:(NSEvent *)theEvent
{
[self resetMouseTracking];
[super scrollWheel:theEvent];
}
- (void) viewDidMoveToWindow:(NSWindow *)newWindow
{
[self resetMouseTracking];
[super viewWillMoveToWindow:newWindow];
}
- (void) updateTrackingAreas
{
[self resetMouseTracking];
[super updateTrackingAreas];
}
但不僅這些嘗試滾動(ALA滾輪:)似乎是不必要期間有不完整的和馬車的結果,跟蹤區域的不斷重新計算。相反,有一個有效的方法來捕獲滾動事件的發生和偏移(在iOS中很容易),這樣我就可以在滾動期間使所有跟蹤區無效。在scrollview的contentView上使用NSViewBoundsDidChangeNotification告訴我滾動正在發生,但並不表示它何時啓動或停止。
從NSScrollView滾動開始和結束通知需要深層次的子類化還是有其他東西我忽略?它有一個完全不同的方法,顯示更多的承諾?
更多類似的比較,我決定使用一個不同的解決方案,在這裏我用一個單一的跟蹤區域上含有NSCollectionView的NSScrollView子類的範圍。在mouseMoved中:我查詢了鼠標在查詢集合視圖及其原型視圖後盤旋的集合視圖項的索引,並從NSClipView中獲取了documentVisibleRect。然後,我通過集合視圖項目發送適當的視圖。剩下的唯一問題是收集視圖項目在滾動期間不會更新。但是,滾動完成後,鼠標懸停在預期的作品上。 – ctpenrose