如何在iOS开发中以编程方式使用接近传感器? [英] How to use proximity sensor programmatically with iOS development?

查看:141
本文介绍了如何在iOS开发中以编程方式使用接近传感器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番谷歌搜索后,我了解到接近传感器"用于在设备远离/靠近用户时打开/关闭屏幕.我观看了这部视频(从30秒开始观看),并对这些很棒的东西感到惊讶.我想在我的应用程序中实现它.

After some googling, I can understand that the "proximity sensor" which is used to on/off screen when the device is away/near from the user. I watched this video (watch from 30th sec) and surprised about this cool stuff. I want to implement it in my app.

但是我知道,当proximityMonitoringEnabledYES时,没有可用的公共API可以保护屏幕锁定.那上面的应用程序该怎么做呢?

But I come to know that there is no public API is available that can protect the screen lock when proximityMonitoringEnabled is YES. Then how can the above app did this?

为清楚起见,我正在复制一些代码.

For clear understanding, I'm copying some code.

启用接近传感器:

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

问题:

当设备靠近用户"状态被调用时,我想显示一些视图.并希望在调用设备距离用户不近"状态时删除视图.

I want to show some view when the "Device is close to user" state was called. And want to remove the view if "Device is not closer to user" state was called.

因此,我添加了一个视图,并在sensorStateMonitor:方法内部将其删除.但是视图仅在几秒钟内可见,并且屏幕关闭.

So I added a view and removed inside the sensorStateMonitor: method. But the view was visible only for some fraction of seconds and the screen goes off.

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

Can I prevent the screen from auto off?

只是困惑!

推荐答案

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

The screen lock can be enabled/disabled.

[UIApplication sharedApplication].idleTimerDisabled = YES;

这篇关于如何在iOS开发中以编程方式使用接近传感器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆