为什么 iPhone 屏幕关闭时摇动不起作用?

发布于 2024-12-02 06:48:09 字数 546 浏览 0 评论 0原文

我正在应用程序中播放音频文件,因此当手机锁定且屏幕关闭时,它不会关闭。 但是,当我想要检测震动时,它不起作用。

当应用程序打开和屏幕锁定(而不是关闭)时,它工作正常。该应用程序肯定正在运行,因为日志工作正常。

有什么想法吗?

我使用以下代码:

-(BOOL)canBecomeFirstResponder {
    //make it respond to shake events
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    //make it respond to shake events
    [self becomeFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventSubtypeMotionShake) {
        NSLog(@"Shake detected");
    }
}

I am playing an audio file in the app, so it doesn't turn off when the phone is locked and the screen is off.
However when I want detect shake it does not work.

It works fine when the app is open and when the screen is locked (not off). The app is definitely running because the logs are working fine.

Any idea?

I use the following code:

-(BOOL)canBecomeFirstResponder {
    //make it respond to shake events
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    //make it respond to shake events
    [self becomeFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventSubtypeMotionShake) {
        NSLog(@"Shake detected");
    }
}

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

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

发布评论

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

评论(3

廻憶裏菂餘溫 2024-12-09 06:48:09

监视加速度计会导致手机额外耗电,因此在手机锁定时禁用它。我不相信有办法解决这个问题。

Monitoring the accelerometer causes extra power consumption by the phone, so it is disabled while the phone is locked. I don't believe there is a way around this.

耳根太软 2024-12-09 06:48:09

这会对你有帮助。

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

这不会让屏幕关闭。

希望这有帮助。

编辑:

但即使我也有和 Akshay 一样的感觉。当屏幕关闭时不可能捕获 UIEvent。

This would help you.

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

This wont let the screen turn off.

Hope this helps.

EDIT:

But Even I feel the same as Akshay does. That it is not possible to capture UIEvent when screen is off.

嗫嚅 2024-12-09 06:48:09

我认为当屏幕关闭时您无法捕获 UIEvents

I don't think you can capture UIEvents when the screen is off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文