0

私がやろうとしているのはNSSliderを持っていることであり、値を変更するたびにNSRectの色を更新したいと思っています。色を変更するたびにNSRectを削除して再描画する必要がありますか、それともどうすればよいですか?Rectを削除して再描画する必要がある場合、どうすればよいですか?

ありがとう!

ここに方法があります

arrowXは単なる整数ですが、スライダーで変更したいのは、[[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];具体的には0と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];
}
4

1 に答える 1

0

を格納するインスタンス変数を追加する必要がありますNSColor。スライダーのアクションメソッドで、インスタンス変数を新しい色に設定setNeedsDisplay:YESし、ビューに送信します。

于 2012-02-20T06:44:38.120 に答える