简体   繁体   中英

On iPhone and iPad, can we draw anything without using drawRect?

It seems that the standard way to draw dots, lines, circles, and Bezier paths is to draw them in inside of drawRect . We don't directly call drawRect , but just let iOS call it and we can use [self setNeedsDisplay] to tell iOS to try to call drawRect when it can...

It also seems that we cannot rely on

[self setClearsContextBeforeDrawing: NO];

to not clear the background of the view before calling drawRect . Some details are in this question: UIView: how to do non-destructive drawing?

How about directly drawing on the screen -- without putting those code in drawRect . For example, in ViewController.m , have some code that directly draw dots, lines, circles on the screen. Is that possible?

Without having to drop into OpenGL, the closest you can do to get around the erasure is to convert the context as an image using something like CGBitmapContextCreateImage . From there, you can retain the image in memory (or write it to disk if necessary), and then when you redraw the view, you first draw this original image into the context and then overlay it with new content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM