iOS開發(fā)-CoreMotion框架(加速計(jì)和陀螺儀)

CoreMotion是一個(gè)專門處理Motion的框架,其中包含了兩個(gè)部分加速度計(jì)和陀螺儀,在iOS4之前加速度計(jì)是由UIAccelerometer類來負(fù)責(zé)采集數(shù)據(jù),現(xiàn)在一般都是用CoreMotion來處理加速度過程,不過由于UIAccelerometer比較簡單,同樣有人在使用。加速計(jì)由三個(gè)坐標(biāo)軸決定,用戶最常見的操作設(shè)備的動(dòng)作移動(dòng),晃動(dòng)手機(jī)(搖一搖),傾斜手機(jī)都可以被設(shè)備檢測到,加速計(jì)可以檢測到線性的變化,陀螺儀可以更好的檢測到偏轉(zhuǎn)的動(dòng)作,可以根據(jù)用戶的動(dòng)作做出相應(yīng)的動(dòng)作,iOS模擬器無法模擬以上動(dòng)作,真機(jī)調(diào)試需要開發(fā)者賬號。

加速計(jì)

加速計(jì)的x,y,z三個(gè)方向,參考下圖:


如果只需要知道設(shè)備的方向,不需要知道具體方向矢量角度,那么可以使用UIDevice進(jìn)行操作,還可以根據(jù)方向就行判斷,具體可以參考一下蘋果官網(wǎng)代碼:

-(void) viewDidLoad {

// Request to turn on accelerometer and begin receiving accelerometer events

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

}

- (void)orientationChanged:(NSNotification *)notification {

// Respond to changes in device orientation

}

-(void) viewDidDisappear {

// Request to stop receiving accelerometer events and turn off accelerometer

[[NSNotificationCenter defaultCenter] removeObserver:self];

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

}

當(dāng)用戶晃動(dòng)設(shè)備的時(shí)候,系統(tǒng)會通知每一個(gè)在用的設(shè)備,可以使本身成為第一響應(yīng)者:

- (BOOL)canBecomeFirstResponder {

return YES;

}

- (void)viewDidAppear:(BOOL)animated {

[self becomeFirstResponder];

}

處理Motion事件有三種方式,開始(motionBegan),結(jié)束(motionEnded),取消(motionCancelled):

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);

motionEnded方法中處理:

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

if (motion == UIEventSubtypeMotionShake)

{

// FlyElephant? http://www.cnblogs.com/xiaofeixiang

[[NSNotificationCenter defaultCenter] postNotificationName:@"FlyElephant" object:self];

}}

CoreMotion在處理加速計(jì)數(shù)據(jù)和陀螺儀數(shù)據(jù)的時(shí)是一個(gè)非常重要的框架,框架本身集成了很多算法獲取原生的數(shù)據(jù),而且能很好的展現(xiàn)出來,CoreMotion與UIKit不同,連接的是UIEvent而不是事件響應(yīng)鏈。CoreMotion相對于接收數(shù)據(jù)只是更簡單的分發(fā)motion事件。

CMMotionManager類能夠使用到設(shè)備的所有移動(dòng)數(shù)據(jù)(motion data),Core Motion框架提供了兩種對motion數(shù)據(jù)的操作方式:

pull方式:能夠以CoreMotionManager的只讀方式獲取當(dāng)前任何傳感器狀態(tài)或是組合數(shù)據(jù);

push方式:是以塊或者閉包的形式收集到想要得到的數(shù)據(jù)并且在特定周期內(nèi)得到實(shí)時(shí)的更新;

pull處理方式:

//判斷加速計(jì)是否可用

if ([_motionManager isAccelerometerAvailable]) {

// 設(shè)置加速計(jì)采樣頻率

[_motionManager setAccelerometerUpdateInterval:1 / 40.0];

[_motionManager startAccelerometerUpdates];

} else {

NSLog(@"博客園-FlyElephant");

}

觸摸結(jié)束:

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

CMAcceleration acceleration=_motionManager.accelerometerData.acceleration;

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

}

push處理方式:

@property (strong,nonatomic) CMMotionManager? *motionManager;

@property (strong,nonatomic) NSOperationQueue *quene;

_motionManager=[[CMMotionManager alloc]init];

//判斷加速計(jì)是否可用

if ([_motionManager isAccelerometerAvailable]) {

// 設(shè)置加速計(jì)頻率

[_motionManager setAccelerometerUpdateInterval:1 / 40.0];

//開始采樣數(shù)據(jù)

[_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

NSLog(@"%f---%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y);

}];

} else {

NSLog(@"博客園-FlyElephant");

}

時(shí)間設(shè)置頻率:

陀螺儀

陀螺儀其實(shí)主要方法和方式和加速計(jì)沒有區(qū)別,先看張陀螺儀旋轉(zhuǎn)的角度圖片:

陀螺儀更新數(shù)據(jù)也有兩種方式,pull方式(startGyroUpdates),push方式(startGyroUpdatesToQueue):

static const NSTimeInterval gyroMin = 0.01;

- (void)startUpdatesWithSliderValue:(int)sliderValue {

// Determine the update interval

NSTimeInterval delta = 0.005;

NSTimeInterval updateInterval = gyroMin + delta * sliderValue;

// Create a CMMotionManager

CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

APLGyroGraphViewController * __weak weakSelf = self;

// Check whether the gyroscope is available

if ([mManager isGyroAvailable] == YES) {

// Assign the update interval to the motion manager

[mManager setGyroUpdateInterval:updateInterval];

[mManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {

[weakSelf.graphView addX:gyroData.rotationRate.x y:gyroData.rotationRate.y z:gyroData.rotationRate.z];

[weakSelf setLabelValueX:gyroData.rotationRate.x y:gyroData.rotationRate.y z:gyroData.rotationRate.z];

}];}

self.updateIntervalLabel.text = [NSString stringWithFormat:@"%f", updateInterval];

}

- (void)stopUpdates{

CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

if ([mManager isGyroActive] == YES) {

[mManager stopGyroUpdates];

}}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,665評論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,627評論 2 380

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