How can I call the iPhone to draw using other method?
I have a view with a class called "drawingViewControl开发者_如何学编程ler", 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.
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:
.
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:
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.
精彩评论