views:

633

answers:

1

When I select "Shake Gesture" from the Hardware menu in the iPhone simulator, my first responder always receives two shake events (with a few milliseconds in between).

Unfortunately these two events in the simulator lead to a problem with an UIScrollview that should scroll to a specific position - what it does, but only for the first event.

So I have the following two questions (that are connected to each other somehow)...

Question 1

Why does the simulator generate two shake events for one "Shake Gesture"?
Note: These two events do not occur when testing on the real device. A shake on the real iPhone always produces one event.

Question 2

Is there a way to make sure two subsequent calls of [myScrollView setContentOffset:CGPointMake(x,0) animated:YES]; get both executed properly?
Note: What I find interesting - when I set animated: to NO in the setContentOffset call, it works flawlessly - both calls are executed! Unfortunately this is not an option for the App I'm working on.

Btw, the problem is easy to reproduce. Just make two calls of setContentOffset with animation to an UIScrollview and find only the first executed...

[myScrollView setContentOffset:CGPointMake(300,0) animated:YES]; //this one works
[myScrollView setContentOffset:CGPointMake(100,0) animated:YES]; //this one not

Thanks in advance!

Best, Markus

A: 

This has to do with the definition of a shake. What you may be calling a shake may be different from the amount of movement the simulator sends. For example, your shake on the device may be on big movement in any axis, whereas the simulator may be 2 big movements. You usually want to handle this in your code with some sort of shake timeout or other logic such that multiple event do not get fired undesirably. You will never know if a user will shake once or shake 5 times in a row, so your logic should be able to handle all possible cases.

coneybeare
Thanks for your thoughts but I think this is not true if you use the functions introduced with 3.0 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event.These ensure you only get one event and exactly these things like "filtering" or "hysteresis" you mention. As stated, it works like a charm on the device itself, no matter how log or often I shake (continously) it is only one event. Unfortunately the simulator behaves differently...
Markus