views:

109

answers:

3

I have a view with a class called "drawingViewController", and I have the drawRect method:

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextMoveToPoint(context, 0.0f, 0.0f);
    CGContextAddLineToPoint(context, 100.0f, 100.0f); 
    CGContextStrokePath(context);


}

But I wanna to define some other drawing method, but it did't work, how can I do so apart from calling drawRect method? thz in advance.

+2  A: 

Why do you want another method? This is the one that gets called by the framework when the client area needs drawing or redrawing.

Just to be clear, you never call drawRect: yourself. You call setNeedsDisplay: and setNeedsDisplayInRect:.

Marcelo Cantos
so, if I want to update the view, I still need to called the drawRect method?
Tattat
To reiterate: *you never call `drawRect:` yourself*. Call the other functions, and the framework will call `drawRect:` for you.
Marcelo Cantos
+1  A: 

drawRect: will be called by the framework to draw the view. So if you want to update the view, you need to indicate the framework that the view needs to be updated by calling setNeedsDisplay: or setNeedsDisplayInRect:

FCo
+1  A: 

As said above, you should only call setNeedsDisplay: and let the system call drawRect: at the appropriate time.

If you need to draw at a time other than when drawRect: is called, such as in a separate thread, maintain an offscreen CGContext that you draw into as needed, then copy the contents to the UIGraphicsGetCurrentContext drawRect: context.

The way to copy one context to another is to set up a CGImage with a CGDataProvider that refers to the same data owned by a CGBitmapContext. Do your drawing in the offscreen CGBitmapContext then use CGContextDrawImage to draw into the other context.

drawnonward