2010-06-17 44 views
7

我有這樣的代碼在我的drawRect方法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沒有清理屏幕,只是寫了上次寫在屏幕上的內容ñ。

回答

5

一旦獲得上下文,請在drawRect:中儘早調用。

CGContextClearRect(context , [self bounds]); 

或者如果設置了clearsContextBeforeDrawing,則調用[super drawRect:];

+4

剛發現設置爲Opaque:NO,UIView會自動刷新drawRect。 – coure2011 2010-06-17 07:58:41