iOS傳感器&&CoreMotion框架

小球游戲

猜猜運(yùn)用以下哪個(gè)傳感器:點(diǎn)這里下載(下載了,記得點(diǎn)星星,謝謝!)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??傳感器的種類

這里主要介紹以下普遍運(yùn)用的六種:

1.距離傳感器(Proximity Sensor)

? ? ? ? 距離傳感器又叫近場(chǎng)傳感器,值得是同一種概念,用于檢測(cè)是否有其他物體靠近設(shè)備屏幕.當(dāng)你打電話或接電話時(shí)將電話屏幕貼近耳邊,iPhone會(huì)自動(dòng)關(guān)閉屏幕 ,好處是節(jié)省電量.

2.磁力計(jì)傳感器(Magnetometer Sensor)

可以感應(yīng)地球磁場(chǎng), 獲得方向信息, 使位置服務(wù)數(shù)據(jù)更精準(zhǔn),可以用于電子羅盤和導(dǎo)航應(yīng)用,iPad的Smart Cover盒蓋睡眠操作就是基于磁力計(jì)傳感器

3.內(nèi)部溫度傳感器(Internal Temperature Sensor)

內(nèi)部溫度傳感器并不是給開發(fā)者用的,而是iPhone檢測(cè)零件溫度過(guò)高的一種措施,從 iPad一代開始,iOS設(shè)備都加入了一個(gè)內(nèi)部溫度傳感器,用于檢測(cè)內(nèi)部組件溫度,當(dāng)溫度超過(guò)系統(tǒng)設(shè)定的閾值時(shí),會(huì)出現(xiàn)提示,如果iPhone內(nèi)部溫度超過(guò)系統(tǒng)設(shè)置門限時(shí)(據(jù)說(shuō)是80℃),就會(huì)自動(dòng)關(guān)機(jī)并且出現(xiàn)如下警告

4.濕度傳感器(Moisture Sensor)

濕度傳感器跟其他基于微電子的傳感器不同,是一個(gè)簡(jiǎn)單的物理傳感器.簡(jiǎn)單來(lái)說(shuō),濕度傳感器就是一張遇水變紅的試紙,Apple的維修人員就是通過(guò)檢測(cè)試紙是否變紅,來(lái)判斷設(shè)備是否進(jìn)水,設(shè)備進(jìn)水不在保修范圍之內(nèi)

5.陀螺儀(Gyroscope)

陀螺儀是隨著iPhone4的上市首次出現(xiàn)在iOS設(shè)備上的傳感器,陀螺儀可以用于檢測(cè)設(shè)備的持握方式,其的原理是檢測(cè)設(shè)備在X、Y、Z軸上所旋轉(zhuǎn)的角速度. 陀螺儀在賽車類游戲中有重大作用:模擬汽車駕駛時(shí)方向盤旋轉(zhuǎn)的動(dòng)作 ,使得這類游戲的操控體驗(yàn)更真實(shí).陀螺儀是飛機(jī)上必不可少的零部件,無(wú)論是真實(shí)的飛機(jī)(戰(zhàn)斗機(jī),客機(jī)),還是幾百塊錢的玩具飛機(jī)或者幾千上萬(wàn)的大疆飛機(jī)

6.加速器(Accelerometer Sensor)

最早出現(xiàn)在iOS設(shè)備上的傳感器之一,加速計(jì)用于檢測(cè)設(shè)備在X、Y、Z軸上的加速度 (哪個(gè)方向有力的作用),加速計(jì)可以用于檢測(cè)設(shè)備的搖晃,經(jīng)典應(yīng)用場(chǎng)景

代碼運(yùn)用:(主要是使用CoreMotion框架)

1.距離傳感器:

- (void)viewDidLoad {

[super viewDidLoad];

/**

proximity:接近

Monitoring:檢測(cè)

*/

//1.開啟進(jìn)場(chǎng)傳感器開關(guān)

//? ? [UIDevice currentDevice].proximityMonitoringEnabled = YES;

//2.開啟進(jìn)場(chǎng)傳感器

//進(jìn)場(chǎng)傳感器屬于iOS系統(tǒng)通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChangeNotification) name:UIDeviceProximityStateDidChangeNotification object:nil];

}

- (void)proximityStateDidChangeNotification{

//1.獲取當(dāng)前設(shè)備的進(jìn)場(chǎng)狀態(tài)

BOOL state = [UIDevice currentDevice].proximityState;

if (state == YES) {

NSLog(@"有逗比靠近你了");

//設(shè)置iPhone屏幕的亮度? 0-1

[[UIScreen mainScreen] setBrightness:0];

}

else{

NSLog(@"逗比被你嚇跑了");

//設(shè)置iPhone屏幕的亮度? 0-1

[[UIScreen mainScreen] setBrightness:0.5];

}

}

2.加速器

#import "ViewController.h"

//核心運(yùn)動(dòng)框架

#import<CoreMotion/CoreMotion.h>

@interface ViewController ()

//運(yùn)動(dòng)管理器(不要使用局部變量,否則會(huì)被釋放導(dǎo)致無(wú)法獲?。?/p>

@property(nonatomic,strong)CMMotionManager *motitonManager;

@end

@implementation ViewController

//運(yùn)動(dòng)管理器(不要使用局部變量,否則會(huì)被釋放導(dǎo)致無(wú)法獲取)

@property(nonatomic,strong)CMMotionManager *motitonManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//核心運(yùn)動(dòng)框架獲取數(shù)據(jù)有兩種方式

/**

pull:我們手動(dòng)獲取,想要獲取直接從運(yùn)動(dòng)管理器的屬性中獲取

push:系統(tǒng)回調(diào)返回?cái)?shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)會(huì)主動(dòng)告訴我們

*/

//? ? [self getPullAccelerometer];

[self getPushAccelerometer];

}

//push方式是系統(tǒng)主動(dòng)告訴我們

-(void)getPushAccelerometer{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isAccelerometerAvailable]){

//如果是push方式則需要3 4 兩步

//3.設(shè)置采集率(讓系統(tǒng)多少s告訴一次我們數(shù)據(jù))? 示例設(shè)置1s,表示每隔1s種系統(tǒng)返回我們一次當(dāng)前加速器數(shù)據(jù)

self.motitonManager.accelerometerUpdateInterval = 1;

//.開始檢測(cè)加速計(jì)數(shù)據(jù)

//4.開始檢測(cè)? push方式是通過(guò)一個(gè)block回調(diào)告知我們數(shù)據(jù)

[self.motitonManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {

//.從加速器獲取中獲取X、Y、Z軸的力度值結(jié)構(gòu)體

CMAcceleration acceleration = accelerometerData.acceleration;

/**

1.當(dāng)手機(jī)處于靜止?fàn)顟B(tài)時(shí),值在 0 -1之間

2.當(dāng)手機(jī)home朝下時(shí)? Y軸為笛卡爾坐標(biāo)系(上正下負(fù))? 當(dāng)手機(jī)home鍵朝右時(shí)? X軸為笛卡爾坐標(biāo)? 當(dāng)手機(jī)home鍵朝上 Z軸為笛卡爾坐標(biāo)系

*/

NSLog(@"x:%f? y:%f? z:%f",acceleration.x,acceleration.y,acceleration.z);

}];

}

}

//pull方式的數(shù)據(jù)需要我們手動(dòng)去獲取

- (void)getPullAccelerometer{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isAccelerometerAvailable]){

//3.開始檢測(cè)加速計(jì)數(shù)據(jù)

//pull方式開啟檢測(cè)之后,硬件會(huì)將檢測(cè)到的加速計(jì)數(shù)據(jù)保存在motitonManager的屬性中,我們想要獲取數(shù)據(jù)需要手動(dòng)獲取

[self.motitonManager startAccelerometerUpdates];

}

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

//1.獲取加速計(jì)數(shù)據(jù)

CMAccelerometerData *accelerometerData = self.motitonManager.accelerometerData;

//2.從加速器獲取中獲取X、Y、Z軸的力度值結(jié)構(gòu)體

CMAcceleration acceleration = accelerometerData.acceleration;

NSLog(@"x:%f? y:%f? z:%f",acceleration.x,acceleration.y,acceleration.z);

}

@end

3.陀螺儀:

#import "ViewController.h"

//核心運(yùn)動(dòng)框架

#import<CoreMotion/CoreMotion.h>

@interface ViewController ()

//運(yùn)動(dòng)管理器(不要使用局部變量,否則會(huì)被釋放導(dǎo)致無(wú)法獲?。?/p>

@property(nonatomic,strong)CMMotionManager *motitonManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//核心運(yùn)動(dòng)框架獲取數(shù)據(jù)有兩種方式

/**

pull:我們手動(dòng)獲取,想要獲取直接從運(yùn)動(dòng)管理器的屬性中獲取

push:系統(tǒng)回調(diào)返回?cái)?shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)會(huì)主動(dòng)告訴我們

*/

//? ? [self getPullGyro];

[self getPushGyro];

}

//push方式是系統(tǒng)主動(dòng)告訴我們

-(void)getPushGyro{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isGyroAvailable]){

//如果是push方式則需要3 4 兩步

//3.設(shè)置采集率(讓系統(tǒng)多少s告訴一次我們數(shù)據(jù))? 示例設(shè)置1s,表示每隔1s種系統(tǒng)返回我們一次當(dāng)前加速器數(shù)據(jù)

self.motitonManager.gyroUpdateInterval = 1;

//.開始檢測(cè)加速計(jì)數(shù)據(jù)

//4.開始檢測(cè)? push方式是通過(guò)一個(gè)block回調(diào)告知我們數(shù)據(jù)

[self.motitonManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {

CMRotationRate rotationRate = gyroData.rotationRate;

/**

靜止?fàn)顟B(tài)下? 值 1 -1 之間

x,y,x軸的坐標(biāo)系是右手法則

手機(jī)圍繞哪一個(gè)軸發(fā)生旋轉(zhuǎn) 則哪一個(gè)軸發(fā)生

//Home 朝下時(shí) 前后翻轉(zhuǎn) Y方法逆時(shí)針為正? ? 左右翻轉(zhuǎn)? Z方向逆時(shí)針為正? 前后翻轉(zhuǎn)? X方向 往里旋轉(zhuǎn)為正

*/

NSLog(@"x:%f? y:%f? z:%f",rotationRate.x,rotationRate.y,rotationRate.z);

}];

}

}

//pull方式的數(shù)據(jù)需要我們手動(dòng)去獲取

- (void)getPullGyro{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isGyroAvailable])

{

//3.開始檢測(cè)加速計(jì)數(shù)據(jù)

//pull方式開啟檢測(cè)之后,硬件會(huì)將檢測(cè)到的加速計(jì)數(shù)據(jù)保存在motitonManager的屬性中,我們想要獲取數(shù)據(jù)需要手動(dòng)獲取

[self.motitonManager startGyroUpdates];

}

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

//1.獲取陀螺儀數(shù)據(jù)

CMGyroData *GyroData = self.motitonManager.gyroData;

//2.從陀螺儀獲取中獲取X、Y、Z軸的角速度值結(jié)構(gòu)體

CMRotationRate rotationRate = GyroData.rotationRate;

NSLog(@"x:%f? y:%f? z:%f",rotationRate.x,rotationRate.y,rotationRate.z);

}


4.磁力計(jì):

#import "ViewController.h"

//核心運(yùn)動(dòng)框架

#import<CoreMotion/CoreMotion.h>

@interface ViewController ()

//運(yùn)動(dòng)管理器(不要使用局部變量,否則會(huì)被釋放導(dǎo)致無(wú)法獲取)

@property(nonatomic,strong)CMMotionManager *motitonManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//核心運(yùn)動(dòng)框架獲取數(shù)據(jù)有兩種方式

/**

pull:我們手動(dòng)獲取,想要獲取直接從運(yùn)動(dòng)管理器的屬性中獲取

push:系統(tǒng)回調(diào)返回?cái)?shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)會(huì)主動(dòng)告訴我們

*/

//? ? [self getPullGyro];

[self getPushGyro];

}

//push方式是系統(tǒng)主動(dòng)告訴我們

-(void)getPushGyro{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isMagnetometerAvailable]){

//如果是push方式則需要3 4 兩步

//3.設(shè)置采集率(讓系統(tǒng)多少s告訴一次我們數(shù)據(jù))? 示例設(shè)置1s,表示每隔1s種系統(tǒng)返回我們一次當(dāng)前加速器數(shù)據(jù)

self.motitonManager.magnetometerUpdateInterval = 1;

//.開始檢測(cè)加速計(jì)數(shù)據(jù)

//4.開始檢測(cè)? push方式是通過(guò)一個(gè)block回調(diào)告知我們數(shù)據(jù)

[self.motitonManager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {

CMMagneticField magneticField = magnetometerData.magneticField;

/**磁場(chǎng)單位有三種

Oe(奧斯特),A/m(高斯),T(特斯拉)三種.

1T=1000mT

1mT=10Oe

1Oe=80A/m

1T(特斯拉)=10000Gs(高斯)=1Wb/M2

1Gs(高斯)=1oe(奧斯特)

*/

/**

iOS磁力計(jì)單位: 特斯拉:T

*/

NSLog(@"x:%f y:%f? z:%f",magneticField.x,magneticField.y,magneticField.z);

}];

}

}

//pull方式的數(shù)據(jù)需要我們手動(dòng)去獲取

- (void)getPullGyro{

//1.創(chuàng)建運(yùn)動(dòng)管理器

self.motitonManager = [[CMMotionManager alloc] init];

//2.檢測(cè)硬件是否可以使用(硬件損壞的情況下不可用)

if( [self.motitonManager isMagnetometerAvailable]){

//3.開始檢測(cè)磁力計(jì)數(shù)據(jù)

//pull方式開啟檢測(cè)之后,硬件會(huì)將檢測(cè)到的加速計(jì)數(shù)據(jù)保存在motitonManager的屬性中,我們想要獲取數(shù)據(jù)需要手動(dòng)獲取

[self.motitonManager startMagnetometerUpdates];

}

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

//1.獲取磁力計(jì)數(shù)據(jù)

CMMagneticField magneticField = self.motitonManager.magnetometerData.magneticField;

NSLog(@"x:%f? y:%f? z:%f",magneticField.x,magneticField.y,magneticField.z);

}

5 . 計(jì)步器

#import "ViewController.h"

#import<CoreMotion/CoreMotion.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self iOS8SetpCounter];

}

- (void)iOS8SetpCounter {

//1. 判斷計(jì)步器是否可用

if (![CMPedometer isStepCountingAvailable]) {

NSLog(@"計(jì)步器不可用");

return;

}

//2. 創(chuàng)建計(jì)步器

CMPedometer *pedometer = [[CMPedometer alloc] init];

//3. 開始計(jì)步統(tǒng)計(jì)? start:從什么時(shí)間開始

//iOS8之后的計(jì)步器可以檢測(cè)距離 但是誤差相當(dāng)大? 每一個(gè)人的每一步距離是不一樣? 實(shí)際開發(fā)中檢測(cè)運(yùn)動(dòng)距離用GPS定位

[pedometer startPedometerUpdatesFromDate:[NSDate date]

withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

//計(jì)步器屬于子線程

NSLog(@"%d",[NSThread isMainThread]);

NSLog(@"%@",[NSThread currentThread]);

//子線程刷新UI,延遲非常大,而且會(huì)存在潛在的閃退

//在子線程中回到主線程刷新UI? UntilDone:是否立即執(zhí)行(YES:同步)

[self performSelectorOnMainThread:@selector(updateUI:) withObject:pedometerData.numberOfSteps waitUntilDone:YES];

NSLog(@"步數(shù)%@",pedometerData.numberOfSteps);

}];

}

- (void)updateUI:(NSNumber *)step{

NSLog(@"步數(shù)======%@",step);

self.label.text = [NSString stringWithFormat:@"%@ 步",step];

}

#pragma mark -iOS7計(jì)步器用法

- (void)iOS7SetpCounter {

//1. 判斷計(jì)步器是否可用:iOS7出現(xiàn)的 , iOS8過(guò)期了

if (![CMStepCounter isStepCountingAvailable]) {

NSLog(@"計(jì)步器不可用");

return;

}

//2. 創(chuàng)建計(jì)步器

CMStepCounter *stepCounter = [[CMStepCounter alloc] init];

//3. 開始計(jì)步統(tǒng)計(jì)? updateOn:步數(shù)更新頻率(每走幾步告訴一次我們數(shù)據(jù))

[stepCounter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {

NSLog(@"numberOfSteps: %zd", numberOfSteps);

}];

}

注:(以下為拓展搖一搖和指紋識(shí)別)

搖一搖:

- (void)viewDidLoad {

[super viewDidLoad];

/**搖一搖功能

第一種實(shí)現(xiàn)方式:使用系統(tǒng)自帶的motionBegan

第二種搖一搖:利用加速器x軸和y軸的力度感應(yīng)可以獲取手機(jī)搖擺的狀態(tài)。再通過(guò)算法計(jì)算出用戶處于搖一搖*/

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

NSLog(@"搖一搖開始");

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

NSLog(@"搖一搖結(jié)束");

}


指紋識(shí)別:

#import "ViewController.h"

#import<LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

- (IBAction)authButtonClick:(id)sender {

//1.創(chuàng)建識(shí)別上下文 iOS8新增

LAContext *context = [[LAContext alloc] init];

//2.判斷當(dāng)前設(shè)備能夠使用指紋識(shí)別

/**

policy:類型

Authentication:認(rèn)證

Biometrics:生物特征識(shí)別

LAPolicyDeviceOwnerAuthenticationWithBiometrics:iOS8指紋識(shí)別

LAPolicyDeviceOwnerAuthentication:iOS9指紋識(shí)別? 9與8唯一的區(qū)別就是輸錯(cuò)密碼3次之后 iOS9會(huì)自動(dòng)彈出輸入密碼界面

*/

if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]){

[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"五一打八折,泰式A套餐只要88" reply:^(BOOL success, NSError * _Nullable error) {

if (error == nil) {

NSLog(@"指紋識(shí)別成功");

//指紋識(shí)別是在子線程中執(zhí)行的,需要注意不要在子線程中刷新UI

NSLog(@"%@",[NSThread currentThread]);

//如果在子線程刷新UI,則會(huì)警告This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.? This will cause an exception in a future release.

//警告意思? 1.刷新UI會(huì)失?。ㄑ舆t非常大 幾十秒)? 2.不可知的崩潰

//異步主線程

dispatch_async(dispatch_get_main_queue(), ^{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"選個(gè)號(hào)吧" delegate:nil cancelButtonTitle:@"考慮一下" otherButtonTitles:@"開始上鐘", nil];

[alertView show];

});

}

else

{

/**code類型

-1 :連續(xù)輸錯(cuò)三次

-8:三次之后再次輸錯(cuò)

-2:用戶點(diǎn)擊取消

-3:用戶點(diǎn)擊輸入密碼

*/

//并不是每一次輸錯(cuò)都會(huì)走失敗方法

NSLog(@"%@",error);

//異步主線程

dispatch_async(dispatch_get_main_queue(), ^{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"沒錢就去找翼老師" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確認(rèn)", nil];

[alertView show];

});

}

}];

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容