为什么 iPhone 屏幕关闭时摇动不起作用?
我正在应用程序中播放音频文件,因此当手机锁定且屏幕关闭时,它不会关闭。 但是,当我想要检测震动时,它不起作用。
当应用程序打开和屏幕锁定(而不是关闭)时,它工作正常。该应用程序肯定正在运行,因为日志工作正常。
有什么想法吗?
我使用以下代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
监视加速度计会导致手机额外耗电,因此在手机锁定时禁用它。我不相信有办法解决这个问题。
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.
这会对你有帮助。
这不会让屏幕关闭。
希望这有帮助。
编辑:
但即使我也有和 Akshay 一样的感觉。当屏幕关闭时不可能捕获 UIEvent。
This would help you.
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.
我认为当屏幕关闭时您无法捕获
UIEvents
。I don't think you can capture
UIEvents
when the screen is off.