CoreMotion框架的使用
CoreMotion框架十分強大,它不僅將加速度傳感器和螺旋儀傳感器進行了統一配置和管理,還為我們封裝了許多算法,我們可以直接獲取到設備的運動狀態信息。
1、CoreMotion負責處理的數據
CoreMotion負責處理四種數據,一種是加速度數據,一種是螺旋儀數據,一種是磁感應數據,還有一種是前三種數據通過復雜運算得到的設備的運動數據。幾個主要的類如下:
CMAccelerommterData:設備的加速度數據
typedef struct {
double x;
double y;
double z;
} CMAcceleration;
@interface CMAccelerometerData : CMLogItem
{
@private
id _internal;
}
//加速度的數據對象
@property(readonly, nonatomic) CMAcceleration acceleration;
@end
CMGyroData:設備的螺旋儀數據
typedef struct {
double x;
double y;
double z;
} CMRotationRate;
@interface CMGyroData : CMLogItem
{
@private
id _internal;
}
//螺旋儀數據對象
@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:設備的運動狀態數據
@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>
2、CoreMotion的使用
{
@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
CoreMotion有兩種使用方式,一種是我們主動向manager索取數據,一種是通過回調讓manager將數據傳給回調給我們,這兩種方式分別稱作pull方式和push方式。
pull方式:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創建管理對象
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{
push方式:
//獲取數據
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的更多屬性和方法
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;
//加速度傳感器數據對象
@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(@"遠距離");
}
}