iPhone的SDK是否可以利用iPhone的距离传感器?如果可以,为什么没有人利用它们呢?我想到了一些不错的用途。
例如,在赛车游戏中,您可以将手指放在接近传感器上,而不是使用拇指占用屏幕空间。当然,如果这是您唯一的选择,那么iPod touch用户将无法使用该应用程序。
接近传感器只能告诉您是否有物体在其前面,而不能告诉您物体与传感器的距离有多远。
这个有公共API可以使用。-[UIApplication setProximitySensingEnabled:(BOOL)]将启用此功能。顺便说一下,它似乎并没有使用光传感器,因为在黑暗的房间中,接近传感会出现问题。
然而,该API调用基本上会在你把手机靠近脸时使屏幕变空白。遗憾的是,对于交互来说没有用处。
假设你指的是当你将手机贴近耳朵时关闭屏幕的传感器,我相信那只是位于耳麦扬声器内部的红外线传感器。如果你启动电话应用程序(不必进行通话),并将某些东西放在耳麦上形成阴影,那么你就可以使显示器关闭。
当你提出这个问题时,它无法通过公共API访问。现在,你可以通过UIDevice的proximityState
属性访问传感器的状态。但是,对于游戏来说并没有太大用处,因为它只是一个开关,而不是近距离/远距离的测量。此外,它只在iPhone上可用,而不是iPod touch。
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
device = [UIDevice currentDevice];
// Turn on proximity monitoring
[device setProximityMonitoringEnabled:YES];
// To determine if proximity monitoring is available, attempt to enable it.
// If the value of the proximityMonitoringEnabled property remains NO, proximity
// monitoring is not available.
// Detect whether device supports proximity monitoring
proxySupported = [device isProximityMonitoringEnabled];
// Register for proximity notifications
[notificationCenter addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:device];
// Returns a BOOL, YES if device is proximate
[device proximityState];
接近传感器通过测量红外反射工作。如果你将iPhone靠近网络摄像头,你可以看到一个小的、闪烁的红外LED。
UIDevice proximityMonitoringEnabled
。这方面暂无公开的API。