0

I have the following code:

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if (motion == UIEventSubtypeMotionShake){
        NSLog(@"Detected a shake"); }
}

This code NSLogs when shake began, or I can implement motionEnded and then it will NSLog when shake ends, but what I want is to execute code DURING shake (progress bar from 0 to 100 while iPhone is shaking).

Can you help me with that please?

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
1337code
  • 169
  • 1
  • 3
  • 11

1 Answers1

0

On the motion began callback you should create a thread which executes a function. That function should do what you want to do "during" the shaking. When the motionEnded callback is called you should terminate the thread.

Look at NSThread documentation for more information.

Most notably look at:

drewag
  • 93,393
  • 28
  • 139
  • 128
  • Yes, but the problem is that motionEnded is not recognized sometimes, it keeps running the function even if you don't shake it – 1337code Oct 22 '12 at 09:25
  • You should also listen for the `-motionCancelled:withEvent:`. Either that or `-motionEnded:withEvent:` will always be called. However looking closer at the documentation it looks like the shake gesture will be canceled if it "goes too long". I can't find a specific figure on how long "too long" is. If that is a problem you will have to listen to the accelerometer data more directly. – drewag Oct 23 '12 at 00:58