傳感器

  • 加速計(jì) 陀螺儀 磁力計(jì) 距離傳感器 計(jì)步器 搖一搖
  • 傳感器是一種感應(yīng) 檢測(cè)裝置
  • 作用:用于感應(yīng) 檢測(cè)設(shè)備周邊信息
    在地圖應(yīng)用中,能判斷出手機(jī)頭面向的方向
    一關(guān)燈,iphone會(huì)自動(dòng)降低亮度,讓屏幕顯得不是那么刺眼
    打電話時(shí),人臉貼近iPhone屏幕時(shí),屏幕會(huì)自動(dòng)鎖屏,達(dá)到省電的目的
  • iphone5內(nèi)置傳感器:
    運(yùn)動(dòng)傳感器 加速度傳感器
    環(huán)境光傳感器 :感應(yīng)周邊環(huán)境光線的強(qiáng)弱(自動(dòng)調(diào)節(jié)屏幕亮度)
    距離傳感器:感應(yīng)是否有其他物體靠近設(shè)備屏幕(打電話自動(dòng)鎖屏)
    磁力計(jì)傳感器:感應(yīng)周邊的磁場(chǎng)(盒蓋鎖屏)
    內(nèi)部溫度傳感器:感應(yīng)設(shè)備內(nèi)部的溫度(提醒用戶降溫,防止設(shè)備損傷)
    濕度傳感器:感應(yīng)設(shè)備是否進(jìn)水(方便維護(hù)人員)
    陀螺儀:感應(yīng)設(shè)備的持握方式(賽車類游戲)
    加速計(jì):感應(yīng)設(shè)備的運(yùn)動(dòng)(搖一搖,計(jì)步器)
    氣壓傳感器:測(cè)量大氣壓的氣壓計(jì)(測(cè)算海拔高度)iphone6新增

搖一搖

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"開始搖手機(jī)");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"結(jié)束搖手機(jī)");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"取消");
}

距離傳感器

    //打開距離傳感器(默認(rèn)是關(guān)閉)
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    //接受距離傳感器的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChangeNotification) name:UIDeviceProximityStateDidChangeNotification object:nil];
-(void)proximityStateDidChangeNotification {
    if([UIDevice currentDevice].proximityState == YES){
        NSLog(@"有人靠近");
    }else {
        NSLog(@"走了");
    }
}

CoreMotion框架的使用

加速計(jì)(Accelerometer)

//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
  • push方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//-----------------------------------------------------------
    //1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    
    //2.判斷加速計(jì)是否可用
    if(self.manager.isAccelerometerAvailable == NO){
        NSLog(@"加速計(jì)不可用");
    }
    
    //3.獲取加速計(jì)數(shù)據(jù)
    
    //push方式
    //設(shè)置數(shù)據(jù)的更新頻率
    self.manager.accelerometerUpdateInterval = 1.0;
    //獲取數(shù)據(jù)
    [self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        if(error){
            NSLog(@"%@",error);
        }
        //1表示一個(gè)重力加速度 g
        NSLog(@"x:%f y:%f z:%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
    }];
  • pull方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//------------------------------------------------------------
//1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    
    //2.判斷加速計(jì)是否可用
    if(self.manager.isAccelerometerAvailable == NO){
        NSLog(@"加速計(jì)不可用");
    }
    
    //3.開始獲取加速計(jì)更新數(shù)據(jù)
    [self.manager startAccelerometerUpdates];
//pull方式
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"x:%f y:%f z:%f",self.manager.accelerometerData.acceleration.x,self.manager.accelerometerData.acceleration.y,self.manager.accelerometerData.acceleration.z);
}

磁力計(jì)(Magnetometer)

//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
  • push方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//-----------------------------------------------------------
    //1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    
    //2.判斷磁力計(jì)是否可用
    if(self.manager.isMagnetometerAvailable == NO){
        NSLog(@"磁力計(jì)不可用");
    }
    
    //3.獲取磁力計(jì)數(shù)據(jù)
    //push方式
    //設(shè)置數(shù)據(jù)更新頻率
    self.manager.magnetometerUpdateInterval = 1.0;
    [self.manager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
        if(error){
            NSLog(@"%@",error);
        }
        //磁力計(jì)的單位:T 特斯拉
        NSLog(@"x:%f y:%f z:%f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z);
    }];
  • pull方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//-----------------------------------------------------------
    //1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    
    //2.判斷磁力計(jì)是否可用
    if(self.manager.isMagnetometerAvailable == NO){
        NSLog(@"磁力計(jì)不可用");
    }
    
    //3.獲取磁力計(jì)數(shù)據(jù)
  
   [self.manager startMagnetometerUpdates];
//pull方式
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"x:%f y:%f z:%f",self.manager.magnetometerData.magneticField.x,self.manager.magnetometerData.magneticField.y,self.manager.magnetometerData.magneticField.z);
}

陀螺儀(Gyro)

//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
  • push方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//-----------------------------------------------------------
    //1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    //2.判斷陀螺儀是否可用
    if(self.manager.isGyroAvailable == NO) {
        NSLog(@"陀螺儀不可用");
    }
    //設(shè)置更新頻率
    self.manager.gyroUpdateInterval = 1.0;
    //獲取數(shù)據(jù)
    [self.manager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
        if(error){
            NSLog(@"%@",error);
        }
        NSLog(@"x:%f y:%f z:%f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);
    }];
  • pull方式
//運(yùn)動(dòng)管理者
@property (nonatomic,strong)CMMotionManager *manager;
//-----------------------------------------------------------
    //1.創(chuàng)建運(yùn)動(dòng)管理者
    CMMotionManager *manager = [[CMMotionManager alloc] init];
    self.manager = manager;
    //2.判斷陀螺儀是否可用
    if(self.manager.isGyroAvailable == NO) {
        NSLog(@"陀螺儀不可用");
    }
    //獲取數(shù)據(jù)
    [self.manager startGyroUpdates];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"x:%f y:%f z:%f",self.manager.gyroData.rotationRate.x,self.manager.gyroData.rotationRate.y,self.manager.gyroData.rotationRate.z);
}

計(jì)步器的使用(CMStepCounter CMPedometer)

  • iOS8.0之前
//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
//計(jì)算步數(shù)
@property(nonatomic,strong)CMStepCounter *stepCounter;
//-------------------------------------------------------------
//1.判斷計(jì)步器是否可用
    if(!CMStepCounter.isStepCountingAvailable){
        NSLog(@"計(jì)步器不可用");
    }
    //2.創(chuàng)建計(jì)算步數(shù)對(duì)象
    self.stepCounter = [[CMStepCounter alloc] init];
    //授權(quán) info.plist NSMotionUsageDescription
    //3.開始計(jì)算步數(shù)
    //Queue:隊(duì)列
    //updateOn:從幾步開始計(jì)算步數(shù)
    [self.stepCounter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:1 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {
        if(error){
            NSLog(@"%@",error);
        }
        self.stepLabel.text = [NSString stringWithFormat:@"%zd",numberOfSteps];
    }];
//計(jì)算步數(shù)授權(quán)
<key>NSMotionUsageDescription</key>
    <string></string>
  • iOS8.0之后
//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
//iOS8之后計(jì)算步數(shù)
@property(nonatomic,strong)CMPedometer *pedometer;
//1.判斷計(jì)步器是否可用
    if(![CMPedometer isStepCountingAvailable]){
        NSLog(@"計(jì)步器不可用");
    }
    
    //2.創(chuàng)建計(jì)算步數(shù)對(duì)象
    self.pedometer = [[CMPedometer alloc] init];
    
    //3.開始計(jì)算步數(shù)
    //FromDate :從何時(shí)開始計(jì)步
    [self.pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
        if(error){
            NSLog(@"%@",error);
        }
        NSLog(@"%@",pedometerData.numberOfSteps);
    }];
//計(jì)算步數(shù)授權(quán)
<key>NSMotionUsageDescription</key>
    <string></string>

小球運(yùn)動(dòng)(傳感器應(yīng)用)

//導(dǎo)入框架
CoreMotion.framework
//導(dǎo)入頭文件
#import <CoreMotion/CoreMotion.h>
//運(yùn)動(dòng)管理者
@property(nonatomic,strong)CMMotionManager *motionManager;
//速度
@property(nonatomic,assign)CGPoint spend;
//小球
@property (weak, nonatomic) IBOutlet UIImageView *ballView;

//-----------------------------------------------------------
//1.創(chuàng)建運(yùn)動(dòng)管理者對(duì)象
    self.motionManager = [[CMMotionManager alloc] init];
    //2.判斷加速計(jì)是否可用
    if(self.motionManager.isAccelerometerAvailable == NO){
        NSLog(@"加速計(jì)不可用");
    }
    //設(shè)置跟新時(shí)間
    self.motionManager.accelerometerUpdateInterval = 1/30.0;
    //3.獲取加速計(jì)數(shù)據(jù)
    [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        if(error){
            NSLog(@"加速度獲取失敗");
        }
        //在這個(gè)block中,時(shí)間是固定的,并且一段時(shí)間就會(huì)調(diào)用這個(gè)block
        NSLog(@"x:%f y:%f z:%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
        //x y 是x軸和y軸的加速度
        //將加速度轉(zhuǎn)換成速度
        _spend.x += accelerometerData.acceleration.x;
        _spend.y -= accelerometerData.acceleration.y;
        //改變小球的位置
        CGRect ballFrame = self.ballView.frame;
        ballFrame.origin.x += _spend.x;
        ballFrame.origin.y += _spend.y;
        //防止小球超出四個(gè)邊界
        //左邊
        if(ballFrame.origin.x <= 0){
            ballFrame.origin.x = 0;
            //添加空氣阻力效果
            _spend.x *= -0.7;
        }
        //右邊
        if(ballFrame.origin.x >= self.view.bounds.size.width - ballFrame.size.width){
            ballFrame.origin.x = self.view.bounds.size.width - ballFrame.size.width;
            _spend.x *= -0.7;
        }
        //上邊
        if(ballFrame.origin.y <= 0){
            ballFrame.origin.y = 0;
            _spend.y *= -0.7;
        }
        //下邊
        if(ballFrame.origin.y >= self.view.bounds.size.height - ballFrame.size.height){
            ballFrame.origin.y = self.view.bounds.size.height - ballFrame.size.height;
            _spend.y *= -0.7;
        }
        //改變frame
        self.ballView.frame = ballFrame;
        
    }];

最后編輯于
?著作權(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)容