使用 iphone 加速度计和 UIEventSubtypeMotionShake 事件同时进行

发布于 2024-09-04 17:50:29 字数 997 浏览 3 评论 0原文

我正在使用加速度计来移动/更改应用程序屏幕上的内容。 我还需要在视图控制器中检测其他一些动画的 UIEventSubtypeMotionShake。

由于我的应用程序是一个简单的基于视图的应用程序,因此只有一个视图控制器充当 UIAccelerometerDelegate 和 FirstResponder (用于检测摇动事件)。 检测到第一个摇动手势后,我不需要通过 [accelerometer didAccelerate] 方法输入加速度计,因此我将加速度计委托设置为 nil。

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
    NSLog(@"shake");
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
        //my shake handling code
    }
}

问题是第一次摇动动作没有被正确检测到。我必须摇动 2 3 次或更多次才能触发 UIEventSubtypeMotionShake 事件,而在将加速度计委托设为 nil 后,可以完美检测到后续摇动。

这一定会发生,因为 UIEventSubtypeMotionShake 又取决于加速计 didAccelerate 事件,这些事件被我的代码覆盖。但我需要使用这两个事件。

另一种方法是处理/检测 [加速度计 didAccelerate] 中的抖动,就像在 3.0 之前必须完成的那样 (正如本问题的一些答案中提到的那样) 但是为什么我必须为现成的东西编写代码?

是否有同时使用加速度计和 UIEventSubtypeMotionShake 的应用程序或代码示例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文