- (void)viewDidLoad {
[super viewDidLoad];
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
// 监听有物品靠近还是离开
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)proximityStateDidChange {
if ([UIDevice currentDevice].proximityState) {
NSLog(@"有物品靠近");
} else {
NSLog(@"有物品离开");
}
}
//push方式
// 创建运动管理者对象
self.manager = [[CMMotionManager alloc] init];
// 判断加速计是否可用
if (!self.manager.isAccelerometerAvailable) {
NSLog(@"加速计不可用");
return;
}
// 设置采样间隔
self.manager.accelerometerUpdateInterval = 0.3;
// 开始采样
[self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
// 当采样到加速计信息就会执行
if (error) return;
// 获取加速计信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f, y:%f ,z:%f", acceleration.x, acceleration.y, acceleration.z);
}];
// 判断陀螺仪是否可用
if (![self.manager isGyroAvailable]) {
NSLog(@"手机该换了");
return;
}
// 设置采样间隔
self.manager.gyroUpdateInterval = 0.3;
// 开始采样
[self.manager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
if (error) return ;
CMRotationRate rate = gyroData.rotationRate;
NSLog(@"x:%f, y:%f, z:%f", rate.x, rate.y, rate.z);
}];