3

在用户可以进入向导的下一阶段之前,我NSSegmentedControl必须选择一个。我可以禁用下一个按钮,迫使用户猜测缺少某些东西,但如果用户在未选择控件上的选项的情况下按下 NEXT,我想将控件闪烁红色,例如色调或边框,以引起注意。

我没有在谷歌上找到一个页面告诉我如何做到这一点。在可可中可以做到这一点吗?我怎么能做到这一点NSSegmentedControl

4

1 回答 1

4

我在Tembo中使用有色分段控件。此处的目的不是引起对控件的注意,而是将其与彩色导航栏融为一体。截图右上角。

我没有从头开始绘制分段控件,而是将标准实现绘制到 NSImage 中,然后在绘制到视图之前对其进行着色。

可以使用相同的原理来引起对控制的注意。每当您更改色调颜色时,您都需要调用 setNeedsDisplay

@interface TintedSegmentedCell : NSSegmentedCell
{
    NSMutableDictionary *_frames;
}

@end


@implementation TintedSegmentedControl

@synthesize tintColor = _tintColor;

- (void)dealloc
{
    [_tintColor release], _tintColor = nil;

    [super dealloc];
}

+ (Class)cellClass
{
    return [TintedSegmentedCell class];
}

@end


@implementation TintedSegmentedCell

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
{
    [_frames setObject:[NSValue valueWithRect:frame] forKey:[NSNumber numberWithInteger:segment]];

    [super drawSegment:segment inFrame:frame withView:controlView];
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view
{
    if ([view isKindOfClass:[TintedSegmentedControl class]]) {
        NSColor *tintColor = [(TintedSegmentedControl*)view tintColor];

        if (tintColor != nil) {
            NSRect bounds = frame;

            bounds.origin.x = 0;
            bounds.origin.y = 0;

            NSSize size = bounds.size;
            NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];

            NSInteger segmentCount = [self segmentCount];
            NSMutableDictionary *frames = [NSMutableDictionary dictionaryWithCapacity:segmentCount];

            _frames = frames;

            [image lockFocus];
            {
                [super drawWithFrame:bounds inView:view];
            }
            [image unlockFocus];

            NSImage *tintedImage = [[image hh_imageTintedWithColor:[NSColor blackColor]] hh_imageTintedWithColor:tintColor];

            [tintedImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            NSImage *overlayImage = [[[NSImage alloc] initWithSize:size] autorelease];

            [overlayImage lockFocus];
            {
                _frames = nil;

                for (NSInteger segment = 0; segment < segmentCount; segment++) {
                    NSRect frameRect = [[frames objectForKey:[NSNumber numberWithInteger:segment]] rectValue];

                    [self drawSegment:segment inFrame:frameRect withView:view];
                }
            }
            [overlayImage unlockFocus];

            [overlayImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            return;
        }
    }

    [super drawWithFrame:frame inView:view];
}

@end
于 2014-08-10T17:20:53.760 回答