drawRect没有刷新屏幕

时间:2010-06-17 07:13:37

标签: objective-c iphone

我的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没有清除屏幕,它只是写了上次在屏幕上写的内容。

1 个答案:

答案 0 :(得分:5)

一旦获得上下文,请在drawRect:中尽早调用。

CGContextClearRect( context , [self bounds] );

如果设置了[super drawRect:];,请致电clearsContextBeforeDrawing