iOS傳感器(指南針、GPS、加速計(jì)、搖一搖)

一:指南針

#import <CoreLocation/CoreLocation.h>

CLLocationManager *locManger = [[CLLocationManager alloc] init];

locManger.delegate = self;

locManger.headingFilter = 0.1;// 最小變化度數(shù)

[locManger startUpdatingHeading];// 開始更新方向

// 代理方法

- (void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading {

? ? if(newHeading.magneticHeading == 0) {// 0度表示北方,180度表示南方

? ? ? ? NSLog(@"北方");

? ? } else if(newHeading.magneticHeading == 180) {

? ? NSLog(@"南方");

}}

二:GPS

#import <CoreLocation/CoreLocation.h>

CLLocationManager *locManger = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;// 設(shè)置委托

[self.locationManager setDesiredAccuracy:CLLocationDistanceMax];// 設(shè)置精度為最優(yōu)

[self.locationManager setDistanceFilter:(100.0)];// 設(shè)置更新的最小距離100米

[self.locationManager startUpdatingLocation];// 開始更新位置信息

回調(diào)方法

- (void)locationManager:(CLLocationManager*)manager

didUpdateLocations:(NSArray*)locations {

CLLocation *nowLocation = [locations lastObject];// 獲取當(dāng)前位置

CLLocationDegrees longitutude = newLocation.coordinate.longitude;// 獲取經(jīng)度

CLLocationDegrees latitude = newLocation.coordinate.latitude;// 獲取緯度

}

// 計(jì)算距離

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { ?

double distance = [newLocation distanceFromLocation:oldLocation]; ? NSLog(@"distance = %lf", distance);

}

三:加速計(jì)

1.獲得加速計(jì)對(duì)象

UIAccelerometer*accelerometer = [UIAccelerometersharedAccelerometer];

accelerometer.delegate=self;// 設(shè)置委托

// 設(shè)置更新間隔時(shí)間

accelerometer.updateInterval=1.0f/30.0f;

2.回調(diào)方法

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {

? self.xLabel.text= [NSString stringWithFormat:@"%f",acceleration.x];// 獲得x軸原始數(shù)據(jù)

? self.yLabel= [NSString stringWithFormat:@"%f",acceleration.y];// 獲得y軸原始數(shù)據(jù)

? self.zLabel= [NSString stringWithFormat:@"%f",acceleration.z];// 獲得z軸原始數(shù)據(jù)

}

四:搖一搖

// 在Controller中重寫此方法,就可以獲取搖一搖事件

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

? if (motion == UIEventSubtypeMotionShake) {

? // 檢測(cè)到了,手機(jī)搖一搖

}}

?著作權(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)容