NSSegmentedCell 子类绘制



我创建了NSSegmentedCell的子类,并实现了drawWithFrame,如下所示:

#import "CustomSegmentedCell.h"
@implementation CustomSegmentedCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    int i=0, count=[self segmentCount];
    NSRect segmentFrame=cellFrame;
    for(i=0; i<count; i++) {
        segmentFrame.size.width=[self widthForSegment:i];
        [NSGraphicsContext saveGraphicsState];
        // Make sure that segment drawing is not allowed to spill out into other segments
        NSBezierPath* clipPath = [NSBezierPath bezierPathWithRect: segmentFrame];
        [clipPath addClip];
        [self drawSegment:i inFrame:segmentFrame withView:controlView];
        [NSGraphicsContext restoreGraphicsState];
        segmentFrame.origin.x+=segmentFrame.size.width;
    }
    _lastDrawRect=cellFrame;
}
@end

问题是首次启动应用程序时没有绘制分段,只有当我在分段控件应该绘制的空白区域上单击鼠标时,它才会可见。请让我知道,我在这里错过了什么。

谢谢

子类和实现drawSegment:inFrame:withView:

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
{
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:8 yRadius:8];
    [path setClip];
    [path setLineWidth:2.5];
    [[NSColor grayColor] setStroke];
    NSColor* bgColr;
    if (segment%2) {
        bgColr = [NSColor blackColor];
    }
    else {
        bgColr = [NSColor redColor];
    }
    [bgColr setFill];
    [NSBezierPath fillRect:frame];  
}

相关内容

  • 没有找到相关文章

最新更新