我的drawRect方法中有这段代码
float aValue = .0167f;
float fValue = 20;
for(int i=1; i<=6; i++)
{
CGContextSelectFont(context, "Arial", fValue, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(context, 0);
CGContextSetTextDrawingMode(context, kCGTextFill);
NSString *hString = [[NSString alloc] initWithFormat:@"%i",i];
CGContextSetRGBFillColor(context, 1, 1, 1, aValue);
CGContextShowTextAtPoint(context, i*25, i*16, [hString UTF8String], [hString length]);
aValue += 0.167;
fValue += 1;
}
我正在使用像这样的NSTimer调用方法
staticTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshUIView) userInfo:nil repeats:YES];
这是refreshUIView
-(void)refreshUIView
{
[self setNeedsDisplay];
}
问题是drawRect没有清除屏幕,它只是写了上次在屏幕上写的内容。
答案 0 :(得分:5)
一旦获得上下文,请在drawRect:
中尽早调用。
CGContextClearRect( context , [self bounds] );
如果设置了[super drawRect:];
,请致电clearsContextBeforeDrawing
。