setNeedsDisplay并不总是调用drawRect

时间:2012-10-04 10:20:03

标签: iphone objective-c ios

我之前看过这个问题,但我找不到答案..

正如预期的那样,程序启动时会自动调用drawRect。但是当我调用[self setNeedsDisplay]时,drawRect不再被调用,我无法理解为什么......

// DrawView.m

[self setNeedsDisplay];

-(void)drawRect:(CGRect)rect{

NSLog(@ "Called_1");

//DRAW A PATH
 }

也许使用self是不正确的。我也尝试过使用DrawImage,但它仍无效。

// DrawView.h

@interface DrawView : UIView {

UIImageView *drawImage;
}

// DrawView.m

-(id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = CGRectMake( 0, 0, self.frame.size.width, self.frame.size.height );
    [self addSubview:drawImage];
}

return self;
}

1 个答案:

答案 0 :(得分:0)

通常,当您想要重绘视图时,您应该调用:

[self setNeedsDisplay: YES];

当然,我从来没有在iOS上构建,在OSX上这个代码每次都有效。另外,例如,如果您希望代理人为名为someView的视图调用重绘:

[someView setNeedsDisplay: YES];

注意:YES是由obj-c定义的宏,它只是值1。