views:

898

answers:

4

We have an app in AppStore Bust~A~Spook we had an issue with. When you tap the screen we use CALayer to find the position of all the views during their animation and if you hit one we start a die sequence. However, there is a noticeable delay, it appears as if the touches are buffered and we we receive the event to late. Is there a way to poll or any better way to respond to touches to avoid this lag time?

This is in a UIView not a UIScrollView

+1  A: 

Are you using a UIScrollView to host all this? There's a property of that called delaysContentTouches. This defaults to YES, which means the view tries to ascertain whether a touch is a scroll gesture or not, before passing it on. You might try setting this to NO and seeing if that helps.

Ben Gottlieb
Thanks, but, no this is hosted in a plain old UIView
A: 

Delayed touches usually indicates a CPU overload. Using a NSTimer for frame-to-frame based action is prone to interfering with the touch handling.

If that's the case for your app, then my advice is very simple: OpenGL.

Kriem
A: 

This is a pretty old post about a seasonal app, so the OP probably isn't still working on this problem, but in case others come across this same problem and find this useful.

I agree with Kriem that CPU overload is a common cause of significant delay in touch processing, though there is a lot of optimization one can do before having to pull out OpenGL. CALayer is quite well optimized for the kinds of problems you're describing here.

We should first check the basics:

  • CALayers added to the main view's layer
  • touchesBegan:withEvent: implemented in the main view
  • When the phase is UITouchPhaseBegan, you call hitTest: on the main view's layer to find the appropriate sub-layer
  • Die sequence starts on the relevant model object, updating the layer.

Then, we can check performance using Instruments. Make sure your CPU isn't overloaded. Does everything run fine in simulator but have trouble on the device?

The problem you're trying to solve is very common, so you should not expect a complex or tricky solution to be required. It is most likely that the design or implementation has a basic flaw and just needs troubleshooting.

Rob Napier
I have this same problem (as I've mentioned in my investigation into a problem I was having http://stackoverflow.com/questions/918734/core-animation-with-contentsrect-jerkiness ). I think it's a real problem with the SDK because even if you do no processing at all, you can get a 200ms delay after touchesBegin before you get the first touchesMoved.
U62
A: 

If you're doing any sort of core-animation animation of the CALayers at the same time as you're hit-testing, you must get the presentationLayer before calling hitTest:, as the positions of the model layers do not reflect what might be on screen, but the positions to which the layers are animating.

Hope that helps.

Andrew Pouliot