开发者

shake gesture doesn't work

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) > kAccelerationT开发者_如何学Pythonhreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
            ...
}


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.


'The Shake Gesture' (the one possible on simulator) is not the custom shake detection with accelerometer but rather a shake event detected by iOS. So you can't use your accelerometer didAccelerate method to detect it. Check the second answer (not the one accepted) to this question to see how to detect the shake gesture.


It works, but not in the simulator as laid out in the other answers. Just disregard that fact, and call the method in simulator by some other means (e.g. a button etc.). I would not recommend using the actual shake event that corresponds to the event you can trigger through the simulator. Unless you want your users throwing their phones all over the place. The built-in shake detection is not very sensitive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜