Using iphone accelerometer AND UIEventSubtypeMotionShake event simultaneousluy.

Posted by lukya on Stack Overflow See other posts from Stack Overflow or by lukya
Published on 2010-06-07T10:37:52Z Indexed on 2010/06/07 10:42 UTC
Read the original article Hit count: 444

Filed under:
|

Hi,

I am using accelerometer to move/change things on the screen in my app. I also need to detect UIEventSubtypeMotionShake in the view controller for some other animations.

As my app is a simple view based app, there is just one view controller which acts as UIAccelerometerDelegate AND FirstResponder (for detecting the shake event). After the first Shake gesture is detected, I don’t need accelerometer inputs through [accelerometer didAccelerate] method so I set the accelerometer delegate to nil.

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
    NSLog(@"shake");
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
        //my shake handling code
    }
}

The problem is that the first shake motion is not being detected correctly. I have to shake 2 3 or more times to trigger the UIEventSubtypeMotionShake event, while the subsequent shakes, after the accelerometer delegate is made nil, are being detected perfectly.

This must be happening because UIEventSubtypeMotionShake in turn depends on the accelerometer didAccelerate events which are being overridden by my code. But I need to use both the events.

Are there any apps or code samples which use both accelerometer and UIEventSubtypeMotionShake simultaneously?

Thanks

Swapnil

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiaccelerometer