Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shake gesture doesn't work

Tags:

xcode

iphone

ios4

I use a code for detect shake and this code work on device but when i use shake gesture on simulator doesn't work why?

i use below code for detect it

#define kAccelerationThreshold      2.2
#define kUpdateInterval         (1.0f/10.0f)

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
        if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
            ...
}

1 Answers

Have a look at the motionEnded: method of UIResponder. You can implement motionEnded on your window, view, or view controller to detect shakes, like this:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.type == UIEventSubtypeMotionShake) 
    {
        // your code here

    }
}

In my app I needed an application-wide shake handler. So I subclassed UIWindow (my app has one window, as do most) and put the handler in that subclass.

like image 84
TomSwift Avatar answered Jun 01 '26 18:06

TomSwift