IOS陀螺儀 參數意義
self.mManager = [[CMMotionManager alloc]init]; self.mManager.deviceMotionUpdateInterval = 0.5; if (self.mManager.gyroAvailable) { [self.mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) { NSLog(@"RotationRate X:%.2lf Y:%.2lf Z:%.2lf ",motion.userAcceleration.x,motion.userAcceleration.y,motion.userAcceleration.z); }]; }
x軸 頭 接近 正數 Y參數
x軸 頭 闊別 負數
y軸 左邊 高 負數 X參數
y軸 右邊 高 正數
上面引見下 CoreMotion框架
CoreMotion是一個專門處置Motion的框架,個中包括了兩個部門加快度計和陀螺儀,在IOS4之前加快度計是由UIAccelerometer類來擔任收集數據,如今普通都是用CoreMotion來處置加快渡過程,不外因為UIAccelerometer比擬簡略,異樣有人在應用。加快計由三個坐標軸決議,用戶最多見的操作裝備的舉措挪動,晃悠手機(搖一搖),傾斜手機都可以被裝備檢測到,加快計可以檢測到線性的變更,陀螺儀可以更好的檢測到偏轉的舉措,可以依據用戶的舉措做出響應的舉措,IOS模仿器沒法模仿以上舉措,真機調試須要開辟者賬號。
加快計
加快計的x,y,z三個偏向,參考下圖:
假如只須要曉得裝備的偏向,不須要曉得詳細偏向矢量角度,那末可使用UIDevice停止操作,還可以依據偏向就行斷定,詳細可以參考一下蘋果官網代碼:
-(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]; }
當用戶晃悠裝備的時刻,體系會告訴每個在用的裝備,可使自己成為第一呼應者:
- (BOOL)canBecomeFirstResponder { return YES; } - (void)viewDidAppear:(BOOL)animated { [self becomeFirstResponder]; }
處置Motion事宜有三種方法,開端(motionBegan),停止(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在處置加快計數據和陀螺儀數據的時是一個異常主要的框架,框架自己集成了許多算法獲得原生的數據,並且能很好的展示出來,CoreMotion與UIKit分歧,銜接的是UIEvent而不是事宜呼應鏈。CoreMotion絕對於吸收數據只是更簡略的分發motion事宜。
CMMotionManager類可以或許應用到裝備的一切挪動數據(motion data),Core Motion框架供給了兩種對motion數據的操作方法:
pull方法:可以或許以CoreMotionManager的只讀方法獲得以後任何傳感器狀況或是組合數據;
push方法:是以塊或許閉包的情勢搜集到想要獲得的數據而且在特定周期內獲得及時的更新;
pull處置方法:
//斷定加快計能否可用 if ([_motionManager isAccelerometerAvailable]) { // 設置加快計采樣頻率 [_motionManager setAccelerometerUpdateInterval:1 / 40.0]; [_motionManager startAccelerometerUpdates]; } else { NSLog(@"博客園-FlyElephant"); }
觸摸停止:
-(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];
//斷定加快計能否可用
if ([_motionManager isAccelerometerAvailable]) {
// 設置加快計頻率
[_motionManager setAccelerometerUpdateInterval:1 / 40.0];
//開端采樣數據
[_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
NSLog(@"%f---%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y);
}];
} else {
NSLog(@"博客園-FlyElephant");
}
時光設置頻率:
陀螺儀
陀螺儀其實重要辦法和方法和加快計沒有差別,先看張陀螺儀扭轉的角度圖片:
陀螺儀更新數據也有兩種方法,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]; } }
感激浏覽,願望能贊助到年夜家,感謝年夜家對本站的支撐!
【IOS 陀螺儀開辟(CoreMotion框架)實例詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!