2011-06-20 89 views
4

我有一個NSScrollView,我想知道當用戶mouseDown在它的NSScroller。當使用NSScroller子類(重寫mouseDown)時,我的NSScrollView的水平滾動條不再被繪製/可見。如果我重寫了我的NSScroller的drawRect(並使用它的超視圖來完成繪圖),它會被預期的NSRect調用,但是沒有任何東西被繪製。我錯過了什麼?NSScroller的子類沒有繪製

  • 實際上,在預期的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覆蓋添加了滾動同步等功能。

回答

0

簡單解決方案...有一個未公開的位標來控制方向和行爲:sFlags.isHorz需要在NSScroller子類中設置爲YES或NO。

下面是一個使用SETFRAME一個不必然推薦的例子:覆蓋:

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

......雖然這並不在OSX 10.7(獅子)工作。在獅子,卷軸是垂直的。 – nubee