Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawRect is not refreshing the screen

I have this code in my drawRect method

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;

        }

I am calling a method using NSTimer like this

staticTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshUIView) userInfo:nil repeats:YES];

here is the refreshUIView

-(void)refreshUIView
{
    [self setNeedsDisplay];
}

Problem is that drawRect is not clearing the screen, its just over writing what have been written last time on screen.

like image 312
coure2011 Avatar asked Jun 17 '10 07:06

coure2011


1 Answers

Call this early in drawRect: once you get the context.

CGContextClearRect( context , [self bounds] );

Or call [super drawRect:]; if clearsContextBeforeDrawing is set.

like image 83
drawnonward Avatar answered Sep 29 '22 08:09

drawnonward