xcode 摇动手势不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4195112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shake gesture doesn't work
提问by TomSwift
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)
...
}
回答by TomSwift
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:
看看 UIResponder 的motionEnded: 方法。您可以在窗口、视图或视图控制器上实现 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 子类化(我的应用程序有一个窗口,大多数情况下)并将处理程序放在该子类中。
回答by Swapnil Luktuke
'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 questionto see how to detect the shake gesture.
“摇动手势”(模拟器上可能出现的那种)不是带有加速度计的自定义摇晃检测,而是由 iOS 检测到的摇晃事件。所以你不能使用你的加速度计 didAccelerate 方法来检测它。检查此问题的第二个答案(不是已接受的答案)以了解如何检测摇动手势。
回答by Joseph Tura
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.
它可以工作,但不能在其他答案中列出的模拟器中使用。忽略这个事实,并通过其他方式(例如按钮等)在模拟器中调用该方法。我不建议使用与您可以通过模拟器触发的事件相对应的实际震动事件。除非你想让你的用户把他们的手机扔得到处都是。内置的抖动检测不是很灵敏。