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:(NSEv开发者_Go百科ent *)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.
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:
-(void)setFrame:(NSRect)nsrcFrame
{
[super setFrame:nsrcFrame];
sFlags.isHoriz = (nsrcFrame.size.width > nsrcFrame.size.height); // otherwise always draws vertically
}
精彩评论