你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS App中挪用iPhone各類感應器的辦法總結

iOS App中挪用iPhone各類感應器的辦法總結

編輯:IOS開發綜合

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
CMMa.netometerData:磁感應信息

typedef struct {
    double x;
    double y;
    double z;
} CMMa.neticField;

@interface CMMa.netometerData : 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>
{
@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];
    // 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{
//獲得數據
    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(@"遠間隔");
    }
}

【iOS App中挪用iPhone各類感應器的辦法總結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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