iOS:如何实现“摇动设备”事件? [英] iOS: How to Implement a "Shake Device" event?

查看:144
本文介绍了iOS:如何实现“摇动设备”事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的应用程序发送摇动设备事件 - 即,当用户摇动设备时会发生某些事情。
我尝试实现:

I want to a "Shake Device" event to my app - i.e., when the user shakes the device something happens. I tried implementing:

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) {
        //something happens
    }
}

它似乎不起作用.......

有谁知道我应该使用哪种方法?

It doesn't seem to work.......
Does anyone knows which method I should use?

推荐答案

尝试使用下面的代码,它对我来说很好。

Try using the below code, it worked fine for me.

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

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
                [super motionEnded:motion withEvent:event];
}
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

这篇关于iOS:如何实现“摇动设备”事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆