Non*_*ist 10 xcode cocoa objective-c nsview nscollectionview
我有NSCollectionView几个NSViews.其中NSView有一个NSBox在选择时改变颜色.我还希望NSBox在悬停时改变颜色.
我继承NSBox并添加了mouseEntered和mouseExited方法.我addTrackingRect在里面使用viewWillMoveToWindow但问题是只有当我第一次选择框所在的子视图时才会发生悬停效果.
此外,只有选中的框会在其上发生悬停效果.如何实现悬停过度效果,使NSView我的所有s NSCollectionView立即显示效果?
您可以updateTrackingAreas在子类中覆盖NSView以完成此行为:
界面
@interface HoverView : NSView
@property (strong, nonatomic) NSColor *hoverColor;
@end
Run Code Online (Sandbox Code Playgroud)
执行
@interface HoverView ()
@property (strong, nonatomic) NSTrackingArea *trackingArea;
@property (assign, nonatomic) BOOL mouseInside;
@end
@implementation HoverView
- (void) drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Draw a white/alpha gradient
if (self.mouseInside) {
[_hoverColor set];
NSRectFill(self.bounds);
}
}
- (void) updateTrackingAreas {
[super updateTrackingAreas];
[self ensureTrackingArea];
if (![[self trackingAreas] containsObject:_trackingArea]) {
[self addTrackingArea:_trackingArea];
}
}
- (void) ensureTrackingArea {
if (_trackingArea == nil) {
self.trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited
owner:self
userInfo:nil];
}
}
- (void) mouseEntered:(NSEvent *)theEvent {
self.mouseInside = YES;
}
- (void) mouseExited:(NSEvent *)theEvent {
self.mouseInside = NO;
}
- (void) setMouseInside:(BOOL)value {
if (_mouseInside != value) {
_mouseInside = value;
[self setNeedsDisplay:YES];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1158 次 |
| 最近记录: |