Using Quartz to draw every second via NSTimer (iPhone)

Posted by stuartloxton on Stack Overflow See other posts from Stack Overflow or by stuartloxton
Published on 2010-04-09T16:53:03Z Indexed on 2010/04/09 17:03 UTC
Read the original article Hit count: 269

Filed under:
|
|

Hi,

I'm relatively new to Objective-C + Quartz and am running into what is probably a very simple issue. I have a custom UIView sub class which I am using to draw simple rectangles via Quartz. However I am trying to hook up an NSTimer so that it draws a new object every second, the below code will draw the first rectangle but will never draw again. The function is being called (the NSLog is run) but nothing is draw.

Code:

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext();

    [self step:self];
    [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(2) target:self selector:@selector(step:) userInfo:nil repeats:TRUE];

}

- (void) step:(id) sender {
    static double trans = 0.5f;

    CGContextSetRGBFillColor(context, 1, 0, 0, trans);
    CGContextAddRect(context, CGRectMake(10, 10, 10, 10));
    CGContextFillPath(context);

    NSLog(@"Trans: %f", trans);

    trans += 0.01;
}

context is in my interface file as:

CGContextRef context;

Any help will be greatly appreciated!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone