绘制NSRect的问题

时间:2011-09-13 13:31:06

标签: objective-c cocoa drawrect

我的问题是,当UpdateRect操作调用drawRect方法时,我的rect不会更新高度!

当我点击按钮时,我希望看到我的矩形高度为20,但仍然是10.为什么?

@implementation Graphic_view

int height = 10; //The height of my rect.

-(IBAction)updateRect:(id)sender {
    height += 10;
    //Calling the drawrect method
    [self performSelector:@selector(drawRect:)];
}

-(void)drawRect:(NSRect)dirtyRect {
    NSLog(@"DrawRect has been called !");
    // Drawing code here.
    NSRect viewBounds = [self bounds];
    NSColor *color = [NSColor orangeColor];
    [colore set];
    NSRectFill(viewBounds);
    NSRect myRect;
    myRect.origin.x = 20;
    myRect.origin.y = 20;
    myRect.size.height = height;
    myRect.size.width = 100;
    NSColor *whiteColor = [NSColor whiteColor];
    [whiteColor set];
    NSRectFill(myRect);
} 

@end

1 个答案:

答案 0 :(得分:7)

你不应该自己打电话给drawRect:。而是致电setNeedsDisplay:

-(IBAction)updateRect:(id)sender {
    height += 10;
    // Schedule the drawrect method
    [self setNeedsDisplay:YES];
}

注意:iOS等价物是setNeedsDisplay,没有参数。

相关问题