iOS開發感應器相關

來源:互聯網
上載者:User

標籤:

手機裡面內建了很多的感應器,例如:光感應器,濕度感應器,溫度感應器,距離感應器等等

//開發感應器相關的東西必須使用真機

//在螺旋儀和加速計所有兩種方式push和pull的方式,push的方式是時時檢測,pull的方式是需要時擷取檢測值

/*

加速計push的使用步驟:

1.建立運動管理者

  _mgr = [[CMMotionManager alloc] init];

 2.判斷手機加速計是否可用

 if (!self.mgr.isAccelerometerAvailable) {

        NSLog(@"加速計不可使用,請更換手機");

        return;

    }

3.設定取樣間隔

self.mgr.accelerometerUpdateInterval = 1.0/30.0;

4.開啟檢測

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

        CMAcceleration  acceleration =  accelerometerData.acceleration;

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

        }];

 

 

2.陀螺儀pull方式使用步驟

1.建立運動管理者

  _mgr = [[CMMotionManager alloc] init];

 2.判斷手機陀螺儀是否可用

  if (!self.mgr.isGyroAvailable) {

        NSLog(@"陀螺儀不可用");

        return;

    }

    3.開啟檢測

  [self.mgr startGyroUpdates];

 4.擷取需要的值

 CMRotationRate   rotationRate =   self.mgr.gyroData.rotationRate;

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

//加速計和陀螺儀的用法基本一致--------------

//運動管理者控制器要去擁有它,否則有可能為局部變數,不能使用.

*/

 

 //#import <CoreMotion/CoreMotion.h>匯入與運動相關架構

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>

 

 

@interface ViewController ()

@property (nonatomic,strong) CMMotionManager * mgr;

@end

 

@implementation ViewController

 

//懶載入建立運動管理者

- (CMMotionManager *)mgr

{

    if (_mgr == nil) {

        _mgr = [[CMMotionManager alloc] init];

    }

    return _mgr;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

   

       

 

}

 

 

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

{

    NSLog(@"%zd,%@",motion,event);

 

    NSLog(@"開始搖晃");

}

 

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

    NSLog(@"%zd,%@",motion,event);

 

    NSLog(@"搖晃取消");

}

 

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

{

    NSLog(@"%zd,%@",motion,event);

    NSLog(@"搖晃結束");

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //加速計相關

//    CMAcceleration   acceleration =    self.mgr.accelerometerData.acceleration;

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

    //加速計相關

    

    //陀螺儀相關

    CMRotationRate   rotationRate =   self.mgr.gyroData.rotationRate;

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

}

 

 

- (void)pullGyro

{

    if (!self.mgr.isGyroAvailable) {

        NSLog(@"陀螺儀不可用");

        return;

    }

    

    [self.mgr startGyroUpdates];

}

 

- (void)pushGyro

{

    //判斷手機的陀螺儀是否可用

    if (!self.mgr.gyroAvailable) {

        NSLog(@"陀螺儀不可用,請更換手機");

        return;

    }

    //設定採樣間隔

    self.mgr.gyroUpdateInterval = 1.0/20.0;

    

    //開始檢測陀螺儀的變化

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

        

        

        CMRotationRate  rotationRate =   self.mgr.gyroData.rotationRate;

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

        

    }];

    

}

 

 

- (void)pullAccelerometer

{

    if (!self.mgr.isAccelerometerAvailable) {

        NSLog(@"加速計不可使用,請更換手機");

        return;

    }

    

    [self.mgr startAccelerometerUpdates];

}

 

 

 

 

- (void)pushAccelerometer

{

    //CMMotionManager * mgr = [[CMMotionManager alloc] init];

    

    //判斷加速計是否可用

    if (!self.mgr.isAccelerometerAvailable) {

        NSLog(@"加速計不可用,請更換手機");

        return;

    }

    

    //設定採樣間隔

    self.mgr.accelerometerUpdateInterval = 1.0/30.0;

    

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

        CMAcceleration  acceleration =  accelerometerData.acceleration;

        

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

        

    }];

 

}

 

- (void)proximityMonitoring

{

    //開啟距離感應器

    [UIDevice currentDevice].proximityMonitoringEnabled = YES;

    

    //監聽距離的變化

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

    

 

}

 

- (void)proximityStateDidChange:(NSNotification*)noti

{

    if ([UIDevice currentDevice].proximityState == YES) {

        NSLog(@"有物品靠近");

    }else{

        NSLog(@"物品離開");

    }

}

 

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

iOS開發感應器相關

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.