iOS App中調用iPhone各種感應器的方法總結
CoreMotion框架的使用
CoreMotion框架十分強大,它不僅將加速度傳感器和螺旋儀傳感器進行了統(tǒng)一配置和管理,還為我們封裝了許多算法,我們可以直接獲取到設備的運動狀態(tài)信息。
1、CoreMotion負責處理的數(shù)據(jù)
CoreMotion負責處理四種數(shù)據(jù),一種是加速度數(shù)據(jù),一種是螺旋儀數(shù)據(jù),一種是磁感應數(shù)據(jù),還有一種是前三種數(shù)據(jù)通過復雜運算得到的設備的運動數(shù)據(jù)。幾個主要的類如下:
CMAccelerommterData:設備的加速度數(shù)據(jù)
typedef struct {
double x;
double y;
double z;
} CMAcceleration;
@interface CMAccelerometerData : CMLogItem
{
@private
id _internal;
}
//加速度的數(shù)據(jù)對象
@property(readonly, nonatomic) CMAcceleration acceleration;
@end
CMGyroData:設備的螺旋儀數(shù)據(jù)
typedef struct {
double x;
double y;
double z;
} CMRotationRate;
@interface CMGyroData : CMLogItem
{
@private
id _internal;
}
//螺旋儀數(shù)據(jù)對象
@property(readonly, nonatomic) CMRotationRate rotationRate;
@end
CMMagnetometerData:磁感應信息
typedef struct {
double x;
double y;
double z;
} CMMagneticField;
@interface CMMagnetometerData : CMLogItem
{
@private
id _internal;
}
//磁力對象
@property(readonly, nonatomic) CMMagneticField magneticField;
@end
CMDeviceMotion:設備的運動狀態(tài)數(shù)據(jù)
@interface CMDeviceMotion : CMLogItem
{
@private
id _internal;
}
//設備的狀態(tài)對象
@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對象中又封裝了許多設備的狀態(tài)屬性:
@interface CMAttitude : NSObject <NSCopying, NSSecureCoding>2、CoreMotion的使用
{
@private
id _internal;
}
//設備的歐拉角roll
@property(readonly, nonatomic) double roll;
//設備的歐拉角pitch
@property(readonly, nonatomic) double pitch;
//設備的歐拉角yaw
@property(readonly, nonatomic) double yaw;
//設備狀態(tài)的旋轉矩陣
@property(readonly, nonatomic) CMRotationMatrix rotationMatrix;
//設備狀態(tài)的四元數(shù)
@property(readonly, nonatomic) CMQuaternion quaternion;
@end
CoreMotion有兩種使用方式,一種是我們主動向manager索取數(shù)據(jù),一種是通過回調讓manager將數(shù)據(jù)傳給回調給我們,這兩種方式分別稱作pull方式和push方式。
pull方式:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創(chuàng)建管理對象
manager= [[CMMotionManager alloc]init];
//開啟加速度更新
[manager startAccelerometerUpdates];
//開啟螺旋儀更新
[manager startGyroUpdates];
//開啟狀態(tài)更新
[manager startMagnetometerUpdates];
//創(chuàng)建定時器
NSTimer * time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updata) userInfo:nil repeats:YES];
time.fireDate = [NSDate distantPast];
}
-(void)updata{push方式:
//獲取數(shù)據(jù)
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);
}
//創(chuàng)建管理對象3、CoreMotion的更多屬性和方法
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);
}];
@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;
//加速度傳感器數(shù)據(jù)對象
@property(readonly, nullable) CMAccelerometerData *accelerometerData __TVOS_PROHIBITED;
//pull方式開始更新加速度數(shù)據(jù)
- (void)startAccelerometerUpdates __TVOS_PROHIBITED;
//push方式更新加速度數(shù)據(jù)
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler __TVOS_PROHIBITED;
//停止更新加速度數(shù)據(jù)
- (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;
//螺旋儀數(shù)據(jù)
@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;
//設備磁力狀態(tài)數(shù)據(jù)
@property(readonly, nullable) CMMagnetometerData *magnetometerData NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//pull方式更新設備磁力狀態(tài)
- (void)startMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//push方式更新設備磁力狀態(tài)
- (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//停止更新設備狀態(tài)
- (void)stopMagnetometerUpdates NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;
//設備狀態(tài)更新幀率
@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手機中內置了距離傳感器,位置在手機的聽筒附近,當我們在打電話的時候靠近聽筒,手機的屏幕會自動熄滅,這就靠距離傳感器來控制。
在我們開發(fā)app時,如果需要,也可以調用距離傳感器的一些接口方法。距離傳感器的接口十分簡單,主要通過通知中心來對距離的改變進行通知。
首先,我們需要開啟距離傳感器應用:
[UIDevice currentDevice].proximityMonitoringEnabled=YES;監(jiān)聽距離改變的通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice) name:UIDeviceProximityStateDidChangeNotification object:nil];在回調方法中,我們可以通過下面這個屬性來監(jiān)聽距離狀態(tài):
-(void)notice{
if ([UIDevice currentDevice].proximityState) {
NSLog(@"近距離");
}else{
NSLog(@"遠距離");
}
}
相關文章
iOS 解決UICollectionView 計算 Cell 大小的問題
本文主要介紹iOS UICollectionView,這里給大家一個實例代碼作為參考,并指出經(jīng)常遇到的問題和解決辦法,希望能幫助有需要的小伙伴2016-07-07
詳解iOS應用開發(fā)中autoresizing尺寸自動適應屬性的用法
這篇文章主要介紹了iOS應用開發(fā)中autoresizing尺寸自動適應屬性的用法,文中講解了使用代碼和Storyboard兩種方式調節(jié)autoresizing的方法,示例代碼為Objective-C,需要的朋友可以參考下2016-03-03
iOS關鍵字static extern const使用示例詳解
這篇文章主要為大家介紹了iOS關鍵字static extern const使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
iOS使用runtime修改文本框(TextField)的占位文字顏色

