NSScroller 的子类不绘图
我有一个 NSScrollView,我想知道用户 mouseDown 何时位于它的 NSScroller 上。当子类化 NSScroller (以覆盖 mouseDown)时,我的 NSScrollView 的水平滚动条不再绘制/可见。如果我覆盖我的 NSScroller 的drawRect(并使用它的超级视图来进行绘图),它会使用预期的 NSRect 被调用,但没有绘制任何内容。我缺少什么?
- 实际上,在预期的 HScroller 区域的右侧似乎有一段 15x15 的垂直滚动条轨道。也许我需要指定 setFrameRotation 或其他东西...尽管如果我在 MyHScroller 的 drawRect 中调用 NSFillRect ,它会绘制预期的矩形 - 如果我旋转 90 度,我的填充只有预期 HScroller 区域左侧的 15x15 。
这是我的 NSScrollView 创建代码:
MyHScroller *pMyHScroller = [[MyHScroller alloc] init];
MyScrollView *pMyScrollView = [[MyScrollView alloc] initWithFrame:nsrcFrame];
// pMyHScroller = nil; // With this line uncommented, the HScroller is drawn perfectly
if (pMyHScroller)
{
[pMyScrollView setHorizontalScroller:pMyHScroller];
}
[pMyScrollView setHasVerticalScroller: NO];
[pMyScrollView setHasHorizontalScroller: YES];
[pMyScrollView setAutohidesScrollers: NO];
[pMyScrollView setBorderType: NSBezelBorder];
[pMyScrollView setAutoresizingMask:0|NSViewMinYMargin];
[pMyScrollView setHorizontalLineScroll: 10];
[pMyScrollView setHorizontalPageScroll: 100];
[pMyScrollView setScrollsDynamically: YES];
这是 NSScroller 子类:
@interface MyHScroller : NSScroller
{
}
- (void)mouseDown:(NSEvent *)eventInfo;
- (void)drawRect:(NSRect)nsrcDirty;
@end
@implementation MyHScroller // NSScroller
- (void)mouseDown:(NSEvent *)eventInfo
{
[super mouseDown:eventInfo];
}
- (void)drawRect:(NSRect)nsrcDirty
{
// This fills the expected HScoller area with yellow
[[NSColor yellowColor] set];
NSRectFill(nsrcDirty);
// This verifies the ends of the rect are visible
{
[[NSColor cyanColor] set];
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(bounds.origin.x , bounds.origin.y )];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+20.0, bounds.origin.y+bounds.size.height/2.0)];
[aPath lineToPoint:NSMakePoint(bounds.origin.x , bounds.origin.y+bounds.size.height )];
[aPath stroke];
}
{
[[NSColor cyanColor] set];
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(bounds.origin.x+bounds.size.width , bounds.origin.y )];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+bounds.size.width-20.0, bounds.origin.y+bounds.size.height/2.0)];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+bounds.size.width , bounds.origin.y+bounds.size.height )];
[aPath stroke];
}
// This draws what appears to be a 15x15 Vertical Scroller track segment
// over the right size of the yellow rect
[super drawRect:nsrcDirty];
}
@end
MyScrollView 只是一个 NSScrollView 覆盖,添加了用于滚动同步的功能。
I have an NSScrollView and I'd like to know when the user mouseDown's on it's NSScroller. When subclassed NSScroller (to override mouseDown), my NSScrollView's horizontal scroller is no longer drawn/visible. If I override the my NSScroller's drawRect (and use it's superview to do the drawing), it gets called with the expected NSRect, but nothing is being drawn. What am I missing?
- Actually, there appears to be a 15x15 segment of a vertical scroller track on the right side of the expected HScroller area. Maybe I need to specify setFrameRotation or something...although if I call NSFillRect in MyHScroller's drawRect, it draws the expected rect - if I rotate by 90 degrees, my fill is only 15x15 on the left side of the expected HScroller area.
Here's my NSScrollView creation code:
MyHScroller *pMyHScroller = [[MyHScroller alloc] init];
MyScrollView *pMyScrollView = [[MyScrollView alloc] initWithFrame:nsrcFrame];
// pMyHScroller = nil; // With this line uncommented, the HScroller is drawn perfectly
if (pMyHScroller)
{
[pMyScrollView setHorizontalScroller:pMyHScroller];
}
[pMyScrollView setHasVerticalScroller: NO];
[pMyScrollView setHasHorizontalScroller: YES];
[pMyScrollView setAutohidesScrollers: NO];
[pMyScrollView setBorderType: NSBezelBorder];
[pMyScrollView setAutoresizingMask:0|NSViewMinYMargin];
[pMyScrollView setHorizontalLineScroll: 10];
[pMyScrollView setHorizontalPageScroll: 100];
[pMyScrollView setScrollsDynamically: YES];
Here is the NSScroller subclass:
@interface MyHScroller : NSScroller
{
}
- (void)mouseDown:(NSEvent *)eventInfo;
- (void)drawRect:(NSRect)nsrcDirty;
@end
@implementation MyHScroller // NSScroller
- (void)mouseDown:(NSEvent *)eventInfo
{
[super mouseDown:eventInfo];
}
- (void)drawRect:(NSRect)nsrcDirty
{
// This fills the expected HScoller area with yellow
[[NSColor yellowColor] set];
NSRectFill(nsrcDirty);
// This verifies the ends of the rect are visible
{
[[NSColor cyanColor] set];
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(bounds.origin.x , bounds.origin.y )];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+20.0, bounds.origin.y+bounds.size.height/2.0)];
[aPath lineToPoint:NSMakePoint(bounds.origin.x , bounds.origin.y+bounds.size.height )];
[aPath stroke];
}
{
[[NSColor cyanColor] set];
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(bounds.origin.x+bounds.size.width , bounds.origin.y )];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+bounds.size.width-20.0, bounds.origin.y+bounds.size.height/2.0)];
[aPath lineToPoint:NSMakePoint(bounds.origin.x+bounds.size.width , bounds.origin.y+bounds.size.height )];
[aPath stroke];
}
// This draws what appears to be a 15x15 Vertical Scroller track segment
// over the right size of the yellow rect
[super drawRect:nsrcDirty];
}
@end
The MyScrollView is just an NSScrollView override with functions added for scrolling synchronization.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的解决方案...有一个未记录的位标志控制方向和行为:sFlags.isHorz 需要在 NSScroller 子类中设置为 YES 或 NO。
这是一个使用 setFrame: 覆盖的不一定推荐的示例:
Simple Solution...there's an undocumented bitflag that controls the orientation and behavior: sFlags.isHorz needs to be set to YES or NO in the NSScroller subclass.
Here's a not-necessarily-recommended example using the setFrame: override: