简体   繁体   English

摇动手势不起作用

[英]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) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
            ...
}

Have a look at the motionEnded: method of UIResponder. 看看UIResponder的motionEnded:方法。 You can implement motionEnded on your window, view, or view controller to detect shakes, like this: 您可以在窗口上实现motionEnded,查看或查看控制器以检测抖动,如下所示:

- (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. 所以我将UIWindow子类化(我的应用程序有一个窗口,就像大多数一样)并将处理程序放在该子类中。

'The Shake Gesture' (the one possible on simulator) is not the custom shake detection with accelerometer but rather a shake event detected by iOS. “Shake Gesture”(可能在模拟器上使用)不是使用加速度计的自定义抖动检测,而是iOS检测到的抖动事件。 So you can't use your accelerometer didAccelerate method to detect it. 所以你不能使用你的加速度计didAccelerate方法来检测它。 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 (eg 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. 内置震动检测不是很敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM