更新NSRect颜色或重绘

时间:2012-02-20 06:24:16

标签: objective-c xcode macos xcode4 nsrect

我想要做的是拥有一个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];
}

1 个答案:

答案 0 :(得分:0)

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