自定义Mac Scrollbars与Cocoa

nan*_*ome 5 cocoa scrollbars objective-c

如何使用cocoa创建自定义滚动条?

dre*_*lax 7

如果你不需要,不要重新发明过多的轮子.如果您只想自定义滚动条的外观,可能更容易继承NSScroller并覆盖各种draw方法.

这是未经测试的代码,但如果您有自己的图像,它应该演示如何自定义旋钮的外观MyKnob.png.


@interface MyScroller : NSScroller
{
    NSImage *knobImage;
}
@end




@implementation MyScroller

- (void) dealloc
{
    [knobImage release];
    [super dealloc];
}

- (id) initWithFrame:(NSRect) frame
{
    self = [super initWithFrame:frame];
    if (!self) return nil;

    knobImage = [[NSImage imageNamed:@"MyKnob.png"] retain];

    return self;
}

- (void) drawKnob
{
    // Work out where exactly to draw the knob
    NSPoint p = NSMakePoint(0.0, 0.0);

    [knobImage drawAtPoint:p fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

@end

Run Code Online (Sandbox Code Playgroud)