background image

从善如流。不过,

iPhone 4 确实是率先使用陀螺仪的手机。

二、常用使用方法
    在我们应用中应用最多有加速度传感器,角度加速度伟感器,这两个主要应用在需要重
力感应的应用中,在公开的

API 中 UIAccelerometer 类实现相应的功能;磁阻传感器主要应

用于需要指示方向的应用中,在公开的

API 中 CLLocationManager 类实现相应的功能,上

述几个使用方法今天不作介绍;而影像传感器和亮度传感器的使用方法,是否有公开的
API,限于本人的学术水平,还未查到使用方法;今天主要介绍近接传感器的用法,代码如
下:

[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(sensorStateChange:)
                                             name:@"UIDeviceProximityStateDidChangeNotification"
                                           object:nil];

-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
    if ([[UIDevice currentDevice] proximityState] == YES) {
        NSLog(@"Device is close to user");
        //在此写接近时,要做的操作逻辑代码
    }else{
        NSLog(@"Device is not close to user");
    }
}