NSScroller的子类没有绘制

时间:2011-06-20 10:41:03

标签: cocoa nsscrollview nsscroller

我有一个NSScrollView,我想知道用户mouseDown上的NSScroller。当子类化NSScroller(覆盖mouseDown)时,我的NSScrollView的水平滚动条不再被绘制/可见。如果我覆盖我的NSScroller的drawRect(并使用它的superview进行绘图),它会被预期的NSRect调用,但是没有任何东西被绘制。我错过了什么?

  • 实际上,在预期的HScroller区域右侧似乎有一个15x15的垂直卷轴轨道段。也许我需要指定setFrameRotation或其他东西......虽然如果我在MyHScroller的drawRect中调用NSFillRect,它会绘制预期的rect - 如果我旋转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覆盖,其中添加了用于滚动同步的功能。

1 个答案:

答案 0 :(得分:0)

简单解决方案......有一个未记录的位标记来控制方向和行为:在NSScroller子类中需要将sFlags.isHorz设置为YES或NO。

以下是使用setFrame的一个不一定推荐的示例:override:

-(void)setFrame:(NSRect)nsrcFrame
{
    [super setFrame:nsrcFrame];
    sFlags.isHoriz = (nsrcFrame.size.width > nsrcFrame.size.height); // otherwise always draws vertically
}