iOS中級(jí) (CoreMotion框架,計(jì)步器,距離傳感器,搖一搖)

CoreMotion

大家可能都聽過陀螺儀 這個(gè)東西 是繼iphone4 之后蘋果推出的特別牛的東西,現(xiàn)在教教大家怎么獲取陀螺儀里的信息來做一些事情

距離傳感器(不是基于CoreMotion)框架

- (void)viewDidLoad {
    [super viewDidLoad];
   //打開傳感器
    [UIDevice currentDevice].proximityMonitoringEnabled =YES;
    
    //監(jiān)聽有物品靠近還是離開
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Change:) name:UIDeviceProximityStateDidChangeNotification object:nil];
    
    
}

-(void)Change:(NSNotificationCenter*)center{
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"物品靠近");
    }else{
    
        NSLog(@"物品離開");
    
    }


}

//別忘了釋放掉
-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

手機(jī)微信的搖一搖 (這個(gè)更簡(jiǎn)單) 都知道TouchBegin方法吧

//開始搖一搖
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    NSLog(@"用戶搖一搖");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

//搖一搖被打斷(比如搖的過程中來電話)

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//搖一搖結(jié)束的時(shí)候操作

}

獲取蘋果計(jì)步器的信息(基于CoreMotion)

#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *mylabel;
@property(nonatomic,strong)CMPedometer *step;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (![CMPedometer isStepCountingAvailable ]) {
        NSLog(@"不可用");
        return;
    }
    //開始計(jì)步
    [self.step startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
        NSLog(@"%@",pedometerData.numberOfSteps);
        self.mylabel.text =[NSString stringWithFormat:@"%@",pedometerData.numberOfSteps];
    }];
    
}

下面的代碼比較長(zhǎng)但是沒有復(fù)雜的邏輯 ,因?yàn)椴皇呛軓?fù)雜就寫得一起了,利用CoreMotion 實(shí)現(xiàn)加速器,陀螺儀,磁力傳感器(用于導(dǎo)航,海航)

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic,strong)CMMotionManager *manger;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//獲取磁力計(jì)傳感器
    //1.判斷磁力計(jì)是否可用
    if (![self.manger isMagnetometerAvailable]) {
        return;
    }
    //2.設(shè)置采樣間隔
    self.manger.magnetometerUpdateInterval =1.0;
    //3開始獲取
    [self.manger startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
        CMMagneticField filed = magnetometerData.magneticField;
        NSLog(@"磁力計(jì) %f,%f,%f",filed.x,filed.y,filed.z);
    }];
    //獲取陀螺儀的方法
    [self getGyro];
    //獲取加速計(jì)的方法
    [self getAccelerometer];
    
}

-(void)getGyro{
//先判斷陀螺儀是否可用
    if (![self.manger isGyroAvailable]) {
        NSLog(@"陀螺儀不可用");
        return;
    }
//2 設(shè)置采樣間隔
    self.manger.gyroUpdateInterval =0.2;
    
    [self.manger startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
        CMRotationRate rate =    gyroData.rotationRate;
        //獲取陀螺儀 三個(gè)xyz值
         NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
    }];


}
-(void)getAccelerometer{
    if (![self.manger isAccelerometerAvailable]) {
        NSLog(@"加速計(jì)不可用");
    }
//設(shè)置采取 時(shí)間間隔
    self.manger.accelerometerUpdateInterval =0.1;
    [self.manger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        // 獲取加速計(jì)信息
        CMAcceleration acceleration = accelerometerData.acceleration;
        NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    }];

}
@end

注意1 以上代碼有的需要真機(jī)測(cè)試 才能運(yùn)行 模擬器不行

注意2 以上代碼 總結(jié)就三步 得到這個(gè)類的對(duì)象(記住一定強(qiáng)引用,你要?jiǎng)?chuàng)建用局部變量CMMotionManager *manger =[CMMotionManager...]類似這種局部變量 是不行的,它會(huì)死掉,不會(huì)一直獲取CoreMotion框架里的信息)得到這個(gè)類之后 設(shè)置時(shí)間間隔 最后開始獲取 就這三部很簡(jiǎn)單

注意3 以上代碼都是不斷獲取的 還有點(diǎn)擊一次獲取一次 或者只獲取一次的方法 這里就不在這顯示 比較簡(jiǎn)單 如果找不到可以給我留言

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容