在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