I'm developing an app that allows for multitouch inputs, and am having trouble drawing using drawRect in my UIView. When more than one finger is on the screen, I need to draw a rect that encompasses all the UITouch inputs. Is there any best practice around this, or a way to draw multiple rects at the same time to avoid drawing a larger part of the screen again and again?
Asked
Active
Viewed 117 times
1
-
Why does your title mention **multithreaded** Core Graphics drawing? Are you really trying to render Core Graphics from multiple threads? – Duncan C Sep 29 '20 at 16:44
-
I know coregraphics rendering doesn't allow for multithreading, just trying to get an efficient way to draw multiple touches simultaneously, or at least the illusion of that. – monotreme Sep 29 '20 at 16:53
-
1Core Graphics (CG) does let you render from background threads if you're careful. Just not multiple threads at once. It's UIKit that's not thead-safe. You really have to know what you're doing if you're going to try to write multi-threaded CG code though. – Duncan C Sep 29 '20 at 17:03
-
When you say "draw a rect that encompasses all the UITouch inputs," what are you drawing here? Do you mean a rectangle (like a selection rect)? Or do you mean you make some modification to the bounded area (brighten it or reveal it or something like that)? Or something else? I suspect the answer here really is "CALayer" but I'm not quite certain what you're trying to draw. – Rob Napier Sep 29 '20 at 20:48
-
What I'd ultimately like is when I drag 4 fingers across the screen, each line is drawn separately from each other at the same time, so I don't have to redraw a huge box and encompasses the location of all four fingers. – monotreme Sep 30 '20 at 22:42
1 Answers
2
UIView has a method setNeedsDisplay(_:)
that takes a rectangle to invalidate. You can call that repeatedly to force update of multiple small rectangular areas. The system will ask the view to re-draw those rectangles.

Duncan C
- 128,072
- 22
- 173
- 272
-
That's the problem, when i need to redraw multiple rectangular areas at the same time, drawRect doesn't allow for multithreading, so how can this be accomplished? Or would the redraws jumping from one line to another be so fast the user wouldn't notice? – monotreme Sep 29 '20 at 16:52
-
3The system should call your `draw(rect:)` method repeatedly, once for each rect that needs updating. It should happen fast enough that it's not visible. (One of the requirements for implementing a custom `draw(rect:)` method is that your implementation be **fast**. – Duncan C Sep 29 '20 at 17:04
-
It's been a very long time since I last tested this, so I'd be happy to know if it's changed, but my memory is that the rect sent to `setNeedsDisplay(_:)` is ignored and `draw(rect:)` is always called with the full rectangle on iOS, unlike Mac. – Rob Napier Sep 29 '20 at 20:43
-
Like you it's been a very long time since I've messed with `draw(rect:)`. You might be right. – Duncan C Sep 29 '20 at 20:45