Using iphone accelerometer AND UIEventSubtypeMotionShake event simultaneousluy
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.
An alternative would be to handle/detect the shake in the [accelerometer didAccelerate] just as it had to be done prior to 3.0 (as mentioned in some answers in this question) but why should i have to code for something thats readily available?
Are there any apps or code samples which use both accelerometer and UIEventSubtypeMotionShake simultaneously?
精彩评论