简体   繁体   English

为什么在iPhone上关闭屏幕后摇晃不起作用?

[英]Why shake doesn't work when the screen is off on iPhone?

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");
    }
}

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. 我不认为有办法解决这个问题。

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. 屏幕关闭时无法捕获UIEvent。

我不认为您可以在屏幕关闭时捕获UIEvents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM