IOS 陀螺仪开发(CoreMotion框架)实例详解

2020-01-18 16:28:16王冬梅

陀螺仪更新数据也有两种方式,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开发频道。