I'm working on a game, and I'm using a CAEAGLLayer
backed UIView
subclass to present the game engine. Touch handling is done using -touchesBegan:withEvent:
et. al.
Everything works fine, except that very rarely, if one of the on-screen con开发者_JAVA技巧trols is tapped rapidly, -touchesBegan:withEvent:
doesn't get called for somewhere between 0.1 and 1-2 seconds. This happens maybe one in 20 times, and only if you tap the screen rapidly (4-5 times) first. It seems to be more likely to happen if I am also holding down another finger on a different control on the screen.
Thinking that it was something to do with my own code, I subclassed UIApplication
so I could add a logging statement to -sendEvent:
. When the laggy touch happens, -sendEvent:
doesn't get called until some period of time after the touch has started, so it the touch handling code inside my UIView
subclass doesn't appear to be at fault.
Does anyone have any idea what's going on here (other than iOS having some obscure bug)? Is there some sort of internal "events queue" that makes event delivery become laggy when it fills up? Has anyone else experienced this?
Touch events are only dispatched in the main UI run loop, and sometimes only when the main run loop goes idle for a bit. So if your app is busy handling several previous touch events in a row without taking a break, the main UI run loop might be saturated, and thus not get any further touch events until done with the current stuff.
Touch events also have time stamps. So you can check if they're coming too fast (faster than your event handlers and resulting UI updates can run), and skip or combine some of the event handlers, as appropriate for your app, if you want the app to stay maximally responsive.
精彩评论