ios - 如何在 iOS 开发中以编程方式使用接近传感器?

标签 ios screen monitoring proximitysensor

经过一些谷歌搜索,我了解到“接近传感器”用于在设备远离/靠近用户时打开/关闭屏幕。我看了this video (从第 30 秒开始观看)并对这个很酷的东西感到惊讶。我想在我的应用中实现它。

但我了解到,当 proximityMonitoringEnabledYES 时,没有可用的公共(public) API 可以保护屏幕锁定。那么上述应用程序是如何做到这一点的呢?

为了清楚理解,我复制了一些代码。

启用接近传感器:

[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];

为传感器变化设置观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateMonitor:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];

最后你可以通过这个方法找到接近传感器的状态:

- (void)sensorStateMonitor:(NSNotificationCenter *)notification
{
    if ([[UIDevice currentDevice] proximityState] == YES)
    {
        NSLog(@"Device is close to user.");
    }

    else
    { 
        NSLog(@"Device is not closer to user.");
    }
}

问题:

我想在调用“设备靠近用户”状态时显示一些 View 。如果调用了“设备离用户不近”状态,则想要删除 View 。

所以我在 sensorStateMonitor: 方法中添加了一个 View 并删除了它。但是 View 只显示了几分之一秒,然后屏幕熄灭了。

我可以防止屏幕自动关闭吗?

只是困惑!!

最佳答案

可以启用/禁用屏幕锁定。

[UIApplication sharedApplication].idleTimerDisabled = YES;

关于ios - 如何在 iOS 开发中以编程方式使用接近传感器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31270887/

相关文章:

ios - 在 Swift 中显示以前创建的 View Controller

iphone - 是否可以找到 iPhone 的刷新率?

java - System.out.println()耗时; Java语句?

maven - Spring 启动 : is there a way to show the POM version number in some endpoint easily?

printing - 物理页面/作业完成时如何从打印机获取通知

ios - 其他语言以支持的语言显示,尽管 iOS 仅支持一种语言

ios - 检测距离 Collection View 中心最近的 Collection View 单元

ios - UIDatePicker的Value Changed事件在第一次改变值时不触发

android - 确定应用程序的窗口/屏幕尺寸

c++ - electron.desktopCapturer 到 cv.VideoCapture [electron, nodejs, opencv4nodejs]