2011-12-16 6 views
0

私は問題を抱えている簡単なテストアプリケーションを持っています。 UIは2つのスライダー(x & y)と、私が赤い点を描くカスタムビューです。現在、ドットは最初の位置に表示されますが、スライダーでは移動しません。 NSLogを使うと、drawRectが呼び出され、xとyのデータが最新のスライダを動かすとわかります。 NSViewの私のサブクラス:NSViewの私のサブクラスは更新/再描画されませんが、-drawRectが呼び出されます

#import <Cocoa/Cocoa.h> 


@interface BallView : NSView { 
    NSRect rect; 
    NSBezierPath *bp2; 
} 
@property (readwrite) NSRect rect; 
@end 

#import "BallView.h" 


@implementation BallView 
@synthesize rect; 
- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
    rect = NSMakeRect(50, 10, 10, 10); 

    } 
    return self; 
} 


- (void)drawRect:(NSRect)dirtyRect { 
    NSLog(@"draw rect: %f, %f", rect.origin.x, rect.origin.y); 
    bp2 = [NSBezierPath bezierPath]; 
    [bp2 appendBezierPathWithOvalInRect: rect]; 
    NSColor *color2 = [NSColor redColor]; 
    [color2 set]; 
    [bp2 fill]; 
} 

アプリデリゲート四角形でスライダの値を取得するには:

-(IBAction) setX:(id)sender{ 
x=[sender floatValue]; 
[ballView setRect: NSMakeRect(x, y, 10, 10)]; 
NSLog(@"set x"); 
} 

-(IBAction) setY:(id)sender{ 
    y= [sender floatValue]; 
    [ballView setRect: NSMakeRect(x, y, 10, 10)]; 
    NSLog(@"set y"); 
} 

答えて

1

あなたはそれを再描画が必要であるとの見解を教えていません。状態変化操作には、

[ballView setNeedsDisplay:YES]; 

を入れてください。私は一般に、この目的のためにセッターを実装し、合成の代わりにビューの内部からこれを呼び出させます。

+0

それでした!あなたがた両方に感謝します! – Link

関連する問題