iOS:如何实现“摇动设备”事件?

发布于 2024-11-25 14:16:37 字数 283 浏览 0 评论 0原文

我想为我的应用程序添加“摇动设备”事件 - 即,当用户摇动设备时会发生一些事情。 我尝试实施:

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

它似乎不起作用......
有谁知道我应该使用哪种方法?

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?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何时共饮酒 2024-12-02 14:16:37

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

- (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;
}

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;
}
一片旧的回忆 2024-12-02 14:16:37

可能来不及回答,但在您的 viewDidLoad 中您需要包含。

[self becomeFirstResponder];

尝试一下。

Might be to late to answer but in your viewDidLoad you need to include.

[self becomeFirstResponder];

Give that a try.

许一世地老天荒 2024-12-02 14:16:37

除了 Taylor 的解决方案之外,还要确保您的 AppDelegate.m 中包含此解决方案。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
    return YES;
}

In addition to Taylor's solution, also make sure that your AppDelegate.m has this in it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文