iOS App中调用iPhone各种感应器的方法总结

2020-01-15 16:01:57刘景俊

//设备运动信息是否激活
@property(readonly, nonatomic, getter=isDeviceMotionActive) BOOL deviceMotionActive __TVOS_PROHIBITED;
//设备运动信息对象
@property(readonly, nullable) CMDeviceMotion *deviceMotion __TVOS_PROHIBITED;
//pull方式开始刷新运动信息
- (void)startDeviceMotionUpdates __TVOS_PROHIBITED;
//push方式开始刷新运动信息
- (void)startDeviceMotionUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler __TVOS_PROHIBITED;
//使用某个参考系
- (void)startDeviceMotionUpdatesUsingReferenceFrame:(CMAttitudeReferenceFrame)referenceFrame NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//push方式开始刷新设备运动信息
- (void)startDeviceMotionUpdatesUsingReferenceFrame:(CMAttitudeReferenceFrame)referenceFrame toQueue:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//停止刷新设备运动信息
- (void)stopDeviceMotionUpdates __TVOS_PROHIBITED;

 

距离传感器的应用
iPhone手机中内置了距离传感器,位置在手机的听筒附近,当我们在打电话的时候靠近听筒,手机的屏幕会自动熄灭,这就靠距离传感器来控制。
在我们开发app时,如果需要,也可以调用距离传感器的一些接口方法。距离传感器的接口十分简单,主要通过通知中心来对距离的改变进行通知。
首先,我们需要开启距离传感器应用:

[UIDevice currentDevice].proximityMonitoringEnabled=YES;
监听距离改变的通知:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice) name:UIDeviceProximityStateDidChangeNotification object:nil];
在回调方法中,我们可以通过下面这个属性来监听距离状态:

-(void)notice{
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"近距离");
    }else{
        NSLog(@"远距离");
    }
}

 
注:相关教程知识阅读请移步到IOS开发频道。