我有一个简单的测试应用程序,我遇到了问题。 UI是两个滑块(x& y)和自定义视图,我在上面绘制一个红点。此时,点将显示在初始位置,但不会随滑块移动。使用NSLog我可以告诉我,当我移动滑块时,drawRect被调用并且x和y数据是当前的。 我的NSView的子类:
#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];
}
要将滑块值设置为在app delegate rect:
-(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");
}
答案 0 :(得分:1)
您没有告诉视图它需要重绘。放
[ballView setNeedsDisplay:YES];
进入状态变异行动。我通常会为此目的实现setter,让他们从视图中调用它,而不是合成它。