您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS App 中呼叫 iPhone 各種感應器的方法總結

編輯:iOS 開發綜合

CoreMotion 框架的使用

CoreMotion 框架十分強大,它不僅將加速度感測器與螺旋儀感測器進行了統一配置和管理,還為我們封裝了許多演算法,讓我們可以直接取得設備的運動狀態資訊。

1、CoreMotion 負責處理的資料

CoreMotion 負責處理四種資料:加速度資料、螺旋儀資料、磁感應資料,以及前三種資料透過複雜運算得到的設備運動資料。幾個主要的類如下:

CMAccelerometerData:設備的加速度資料

typedef struct { double x; double y; double z; } CMAcceleration; @interface CMAccelerometerData : CMLogItem { @private id _internal; } //加速度的資料物件 @property(readonly, nonatomic) CMAcceleration acceleration; @endCMGyroData:設備的螺旋儀資料

typedef struct { double x; double y; double z; } CMRotationRate; @interface CMGyroData : CMLogItem { @private id _internal; } //螺旋儀資料物件 @property(readonly, nonatomic) CMRotationRate rotationRate; @endCMMagnetometerData:磁感應資訊

typedef struct { double x; double y; double z; } CMMagneticField; @interface CMMagnetometerData : CMLogItem { @private id _internal; } //磁力物件 @property(readonly, nonatomic) CMMagneticField magneticField; @endCMDeviceMotion:設備的運動狀態資料

@interface CMDeviceMotion : CMLogItem { @private id _internal; } //設備的狀態物件 @property(readonly, nonatomic) CMAttitude *attitude; //設備的角速度 @property(readonly, nonatomic) CMRotationRate rotationRate; //設備的重力加速度 @property(readonly, nonatomic) CMAcceleration gravity; //用戶施加給設備的加速度,設備的總加速度為重力加速度加上用戶給的加速度 @property(readonly, nonatomic) CMAcceleration userAcceleration; //設備的磁場向量物件 @property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);相比前面兩個類,這個就複雜得多,attitude 物件中又封裝了許多設備狀態屬性:

@interface CMAttitude : NSObject <NSCopying, NSSecureCoding> { @private id _internal; } //設備的歐拉角 roll @property(readonly, nonatomic) double roll; //設備的歐拉角 pitch @property(readonly, nonatomic) double pitch; //設備的歐拉角 yaw @property(readonly, nonatomic) double yaw; //設備狀態的旋轉矩陣 @property(readonly, nonatomic) CMRotationMatrix rotationMatrix; //設備狀態的四元數 @property(readonly, nonatomic) CMQuaternion quaternion; @end

2、CoreMotion 的使用

CoreMotion 有兩種使用方式:一種是我們主動向 manager 索取資料,另一種是透過回調讓 manager 把資料回傳給我們,這兩種方式分別稱為 pull 方式與 push 方式。

pull 方式:

- (void)viewDidLoad { [super viewDidLoad]; //建立管理物件 manager= [[CMMotionManager alloc]init]; //開啟加速度更新 [manager startAccelerometerUpdates]; //開啟螺旋儀更新 [manager startGyroUpdates]; //開啟狀態更新 [manager startMagnetometerUpdates]; //建立計時器 NSTimer * time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updata) userInfo:nil repeats:YES]; time.fireDate = [NSDate distantPast]; } -(void)updata{ //取得資料 NSLog(@"%f,%f,%f\n%f,%f,%f",manager.accelerometerData.acceleration.x,manager.accelerometerData.acceleration.y,manager.accelerometerData.acceleration.z,manager.gyroData.rotationRate.x,manager.gyroData.rotationRate.y,manager.gyroData.rotationRate.z); }push 方式:

//建立管理物件 manager= [[CMMotionManager alloc]init]; //在當前執行緒中回調 [manager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) { NSLog(@"%f,%f,%f\n%f,%f,%f",manager.accelerometerData.acceleration.x,manager.accelerometerData.acceleration.y,manager.accelerometerData.acceleration.z,manager.gyroData.rotationRate.x,manager.gyroData.rotationRate.y,manager.gyroData.rotationRate.z); }];

3、CoreMotion 的更多屬性與方法

@interface CMMotionManager : NSObject { @private id _internal; } //設定加速度感測器更新幀率 @property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval __TVOS_PROHIBITED; //加速度感測器是否可用 @property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable __TVOS_PROHIBITED; //加速度感測器是否啟用 @property(readonly, nonatomic, getter=isAccelerometerActive) BOOL accelerometerActive __TVOS_PROHIBITED; //加速度感測器資料物件 @property(readonly, nullable) CMAccelerometerData *accelerometerData __TVOS_PROHIBITED; //pull 方式開始更新加速度資料 - (void)startAccelerometerUpdates __TVOS_PROHIBITED; //push 方式更新加速度資料 - (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler __TVOS_PROHIBITED; //停止更新加速度資料 - (void)stopAccelerometerUpdates __TVOS_PROHIBITED; //螺旋儀感測器更新幀率 @property(assign, nonatomic) NSTimeInterval gyroUpdateInterval __TVOS_PROHIBITED; //螺旋儀是否可用 @property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable __TVOS_PROHIBITED; //螺旋儀是否啟用 @property(readonly, nonatomic, getter=isGyroActive) BOOL gyroActive __TVOS_PROHIBITED; //螺旋儀資料 @property(readonly, nullable) CMGyroData *gyroData __TVOS_PROHIBITED; //pull 方式開始更新螺旋儀 - (void)startGyroUpdates __TVOS_PROHIBITED; //push 方式開始更新螺旋儀 - (void)startGyroUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler __TVOS_PROHIBITED; //停止更新螺旋儀 - (void)stopGyroUpdates __TVOS_PROHIBITED; //磁力感測器更新幀率 @property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //設備磁力感測器是否可用 @property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //設備磁力感測器是否啟用 @property(readonly, nonatomic, getter=isMagnetometerActive) BOOL magnetometerActive NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //設備磁力狀態資料 @property(readonly, nullable) CMMagnetometerData *magnetometerData NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //pull 方式更新設備磁力狀態 - (void)startMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //push 方式更新設備磁力狀態 - (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //停止更新設備狀態 - (void)stopMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //設備狀態更新幀率 @property(assign, nonatomic) NSTimeInterval deviceMotionUpdateInterval __TVOS_PROHIBITED; //參考系列舉 + (CMAttitudeReferenceFrame)availableAttitudeReferenceFrames NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; @property(readonly, nonatomic) CMAttitudeReferenceFrame attitudeReferenceFrame NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED; //設備運動資訊是否可用 @property(readonly, nonatomic, getter=isDeviceMotionAvailable) BOOL deviceMotionAvailable __TVOS_PROHIBITED; //設備運動資訊是否啟用 @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(@"遠距離"); } }

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved