標籤:
手機裡面內建了很多的感應器,例如:光感應器,濕度感應器,溫度感應器,距離感應器等等
//開發感應器相關的東西必須使用真機
//在螺旋儀和加速計所有兩種方式push和pull的方式,push的方式是時時檢測,pull的方式是需要時擷取檢測值
/*
加速計push的使用步驟:
1.建立運動管理者
_mgr = [[CMMotionManager alloc] init];
2.判斷手機加速計是否可用
if (!self.mgr.isAccelerometerAvailable) {
NSLog(@"加速計不可使用,請更換手機");
return;
}
3.設定取樣間隔
self.mgr.accelerometerUpdateInterval = 1.0/30.0;
4.開啟檢測
[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f",acceleration.x,acceleration.y,acceleration.z);
}];
2.陀螺儀pull方式使用步驟
1.建立運動管理者
_mgr = [[CMMotionManager alloc] init];
2.判斷手機陀螺儀是否可用
if (!self.mgr.isGyroAvailable) {
NSLog(@"陀螺儀不可用");
return;
}
3.開啟檢測
[self.mgr startGyroUpdates];
4.擷取需要的值
CMRotationRate rotationRate = self.mgr.gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f",rotationRate.x,rotationRate.y,rotationRate.z);
//加速計和陀螺儀的用法基本一致--------------
//運動管理者控制器要去擁有它,否則有可能為局部變數,不能使用.
*/
//#import <CoreMotion/CoreMotion.h>匯入與運動相關架構
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (nonatomic,strong) CMMotionManager * mgr;
@end
@implementation ViewController
//懶載入建立運動管理者
- (CMMotionManager *)mgr
{
if (_mgr == nil) {
_mgr = [[CMMotionManager alloc] init];
}
return _mgr;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"%zd,%@",motion,event);
NSLog(@"開始搖晃");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"%zd,%@",motion,event);
NSLog(@"搖晃取消");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"%zd,%@",motion,event);
NSLog(@"搖晃結束");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//加速計相關
// CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
// NSLog(@"x:%f y:%f z:%f",acceleration.x,acceleration.y,acceleration.z);
//加速計相關
//陀螺儀相關
CMRotationRate rotationRate = self.mgr.gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f",rotationRate.x,rotationRate.y,rotationRate.z);
}
- (void)pullGyro
{
if (!self.mgr.isGyroAvailable) {
NSLog(@"陀螺儀不可用");
return;
}
[self.mgr startGyroUpdates];
}
- (void)pushGyro
{
//判斷手機的陀螺儀是否可用
if (!self.mgr.gyroAvailable) {
NSLog(@"陀螺儀不可用,請更換手機");
return;
}
//設定採樣間隔
self.mgr.gyroUpdateInterval = 1.0/20.0;
//開始檢測陀螺儀的變化
[self.mgr startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
CMRotationRate rotationRate = self.mgr.gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f",rotationRate.x,rotationRate.y,rotationRate.z);
}];
}
- (void)pullAccelerometer
{
if (!self.mgr.isAccelerometerAvailable) {
NSLog(@"加速計不可使用,請更換手機");
return;
}
[self.mgr startAccelerometerUpdates];
}
- (void)pushAccelerometer
{
//CMMotionManager * mgr = [[CMMotionManager alloc] init];
//判斷加速計是否可用
if (!self.mgr.isAccelerometerAvailable) {
NSLog(@"加速計不可用,請更換手機");
return;
}
//設定採樣間隔
self.mgr.accelerometerUpdateInterval = 1.0/30.0;
[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f y:%f z:%f",acceleration.x,acceleration.y,acceleration.z);
}];
}
- (void)proximityMonitoring
{
//開啟距離感應器
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
//監聽距離的變化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)proximityStateDidChange:(NSNotification*)noti
{
if ([UIDevice currentDevice].proximityState == YES) {
NSLog(@"有物品靠近");
}else{
NSLog(@"物品離開");
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
iOS開發感應器相關