如何在iOS开发中以编程方式使用接近传感器?
通过谷歌搜索,我了解到当设备远离/靠近用户时,用于打开/关闭屏幕的“接近传感器”。我看了(从第30秒开始),对这个很酷的东西感到惊讶。我想在我的应用程序中实现它 但我知道,当如何在iOS开发中以编程方式使用接近传感器?,ios,screen,monitoring,proximitysensor,Ios,Screen,Monitoring,Proximitysensor,通过谷歌搜索,我了解到当设备远离/靠近用户时,用于打开/关闭屏幕的“接近传感器”。我看了(从第30秒开始),对这个很酷的东西感到惊讶。我想在我的应用程序中实现它 但我知道,当proximityMonitoringEnabled为YES时,没有可用的公共API可以保护屏幕锁定。那么上面的应用程序怎么能做到这一点呢 为了清楚地理解,我复制了一些代码 启用接近传感器: [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; [[NS
proximityMonitoringEnabled
为YES
时,没有可用的公共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.");
}
}
设置传感器更改观察员:
[[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.");
}
}
最后,您可以通过此方法找到接近传感器的状态:
[[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.");
}
}
问题:
[[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.");
}
}
我想在调用“设备接近用户”状态时显示一些视图。如果调用了“设备不靠近用户”状态,则要删除该视图
因此,我添加了一个视图,并在sensorStateMonitor:
方法中删除了它。但是,该视图仅在几秒钟内可见,屏幕关闭
我可以防止屏幕自动关闭吗
只是糊涂了 可以启用/禁用屏幕锁定
[UIApplication sharedApplication].idleTimerDisabled=YES;
即使在NSLog旁边包含此代码(@“设备离用户很近”);不起作用。但屏幕还是会自动锁定。看起来这可能有一个bug。尝试在ApplicationIDFinishLaunching
中或在[[UIDevice currentDevice]setProximityMonitoringEnabled:YES]之后进行设置代码>首先选择“否”,然后选择“是”。检查此链接:我在上一条评论之前就尝试过了。这也无济于事:(嗨@Middle,我怎样才能防止屏幕自动关闭?有什么解决办法吗?如果有,你能帮我一下吗。