欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS 陀螺儀開發(fā)(CoreMotion框架)實例詳解

 更新時間:2016年10月11日 09:41:43   投稿:lqh  
這篇文章主要介紹了IOS 陀螺儀開發(fā)實例詳解的相關(guān)資料,介紹了螺旋儀參數(shù)意義及CoreMotion框架,需要的朋友可以參考下

iOS陀螺儀 參數(shù)意義

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軸 頭 靠近 負(fù)數(shù) Y參數(shù)
x軸 頭 遠(yuǎn)離 正數(shù)

y軸 左側(cè) 高 正數(shù) X參數(shù)
y軸 右側(cè) 高 負(fù)數(shù)

下面介紹下  CoreMotion框架

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

加速計

加速計的x,y,z三個方向,參考下圖:

如果只需要知道設(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)用戶晃動設(shè)備的時候,系統(tǒng)會通知每一個在用的設(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在處理加速計數(shù)據(jù)和陀螺儀數(shù)據(jù)的時是一個非常重要的框架,框架本身集成了很多算法獲取原生的數(shù)據(jù),而且能很好的展現(xiàn)出來,CoreMotion與UIKit不同,連接的是UIEvent而不是事件響應(yīng)鏈。CoreMotion相對于接收數(shù)據(jù)只是更簡單的分發(fā)motion事件。

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

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

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

pull處理方式:

//判斷加速計是否可用
if ([_motionManager isAccelerometerAvailable]) {
  // 設(shè)置加速計采樣頻率
  [_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];
//判斷加速計是否可用
if ([_motionManager isAccelerometerAvailable]) {
    // 設(shè)置加速計頻率
    [_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è)置頻率:

 

陀螺儀

陀螺儀其實主要方法和方式和加速計沒有區(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];
   }
}

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • iOS實現(xiàn)圖片折疊效果

    iOS實現(xiàn)圖片折疊效果

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)圖片折疊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • iOS實現(xiàn)無限循環(huán)輪播圖效果

    iOS實現(xiàn)無限循環(huán)輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)無限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

    iOS開發(fā)實戰(zhàn)之Label全方位對齊的輕松實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于iOS開發(fā)實戰(zhàn)之輕松實現(xiàn)Label全方位對齊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • iOS二維碼的生成和掃描

    iOS二維碼的生成和掃描

    這篇文章主要為大家詳細(xì)介紹了iOS二維碼生成和掃描的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • iOS中如何判斷中英文混合的字符長度

    iOS中如何判斷中英文混合的字符長度

    本文通過兩種方法給大家介紹了判斷中英文混合的字符長度,非常具有參考價值,特此分享供大家學(xué)習(xí)
    2016-05-05
  • iOS開發(fā)中實現(xiàn)顯示gif圖片的方法

    iOS開發(fā)中實現(xiàn)顯示gif圖片的方法

    這篇文章主要介紹了iOS開發(fā)中實現(xiàn)顯示gif圖片的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-09-09
  • iOS開發(fā)學(xué)習(xí) ViewController使用示例詳解

    iOS開發(fā)學(xué)習(xí) ViewController使用示例詳解

    這篇文章主要為大家介紹了iOS開發(fā)學(xué)習(xí) ViewController使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • IOS10 相冊相機(jī)閃退bug解決辦法

    IOS10 相冊相機(jī)閃退bug解決辦法

    這篇文章主要介紹了IOS10 相冊相機(jī)閃退bug解決辦法的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • iOS利用余弦函數(shù)實現(xiàn)卡片瀏覽工具

    iOS利用余弦函數(shù)實現(xiàn)卡片瀏覽工具

    這篇文章主要為大家詳細(xì)介紹了iOS利用余弦函數(shù)實現(xiàn)卡片瀏覽工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • IOS 上架后出現(xiàn)90034代碼錯誤問題解決

    IOS 上架后出現(xiàn)90034代碼錯誤問題解決

    這篇文章主要介紹了IOS 上架后出現(xiàn)90034代碼錯誤問題解決的相關(guān)資料,需要的朋友可以參考下
    2016-11-11

最新評論