0

我想要做的是拥有一个 NSSlider,每当我更改值时,我希望它更新我的 NSRect 的颜色。每次我想更改颜色时,我是否需要删除我的 NSRect 并重新绘制它,或者我将如何处理?如果我必须删除 Rect 并重新绘制它,我会怎么做?

谢谢!

继承人的方法

arrowX 只是一个整数,但我想用滑块更改的是[[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];0 到 1 之间的 alpha 值

- (void)drawRect:(NSRect)theRect
{
    NSRect contentRect = NSInsetRect([self bounds], 0, 0);
    NSBezierPath *path = [NSBezierPath bezierPath];

    [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))];
    [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
    [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))];

    NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect));
    [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))
         controlPoint1:topRightCorner controlPoint2:topRightCorner];

    [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))];

    NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect));
    [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))
         controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner];

    [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))];

    [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))
         controlPoint1:contentRect.origin controlPoint2:contentRect.origin];

    [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))];

    NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect));
    [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))
         controlPoint1:topLeftCorner controlPoint2:topLeftCorner];

    [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
    [path closePath];

    //SETTING THE VALUE OF 1 = WHITE AND 0 = BLACK.
    [[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];
    [path fill];

    [NSGraphicsContext saveGraphicsState];

    NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]];
    [clip appendBezierPath:path];
    [clip addClip];

    [NSGraphicsContext restoreGraphicsState];
}
4

1 回答 1

0

您需要添加一个实例变量来存储您的NSColor. 在滑块的操作方法中,将实例变量设置为新颜色,然后发送setNeedsDisplay:YES到视图。

于 2012-02-20T06:44:38.120 回答