0

what I am trying to do is have an NSSlider and whenever I change the value I want it to update the color of my NSRect. Will I need to remove my NSRect and redraw it every time I want to change the color or how would I go about this? If I have to remove the Rect and redraw it how would I do that?

Thanks!

Heres the Method

arrowX is just an integer but what I want to change with the slider is the [[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill]; specifically the alpha value between 0 and 1

- (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];
}
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38

1 Answers1

0

You need to add an instance variable that stores your NSColor. In your slider's action method, set the instance variable to the new color, and send setNeedsDisplay:YES to the view.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • so I made the NSColor *theColor. How would I modify the current line `[[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];` to store only the alpha? or how do I just rewrite it? Thanks so much! – Grant Wilkinson Feb 20 '12 at 06:58
  • The line should be `[theColor setFill]`. You don't store “only the alpha”. You are redrawing the entire contents of the view. – rob mayoff Feb 20 '12 at 07:00
  • ok thanks! everything works except for the setNeedsDisplay, is there something that needs to be changed in interface builder or added to the action? – Grant Wilkinson Feb 20 '12 at 18:59