2012-02-20 11 views
0

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

ありがとうございます!

相続法

arrowXは単なる整数ですが、私はスライダーで変更することはあなたはインスタンス変数を追加する必要があり、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]; 
} 
+0

今NSRectを描画するメソッドを表示します。それは 'drawRect:'ですか? –

+0

はいそれはdrawRectです。私はコード –

答えて

0

[[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];特にアルファ値でありますあなたのNSColorを保存しますスライダのアクションメソッドで、インスタンス変数を新しい色に設定し、setNeedsDisplay:YESをビューに送信します。

+0

で質問を更新しましたので、NSColor * theColorを作成しました。どのように私は現在の行 '[[NSColor colorWithDeviceWhite:0 alpha:0.4] setFill];'をアルファのみを格納するように変更するのですか?どのように私はそれを書き直すだけですか?本当にありがとう! –

+0

行は '[theColor setFill]'でなければなりません。あなたは "アルファのみ"を保存しません。ビューの内容全体を再描画しています。 –

+0

ありがとう!すべてがsetNeedsDisplayを除いて動作しますが、インターフェースビルダーで変更する必要があるか、アクションに追加する必要がありますか? –

関連する問題