How to get accelerometer data in IOS?

我是研究僧i 提交于 2019-12-03 16:58:47

问题


I am using the UIAccelerotmeterDelegate method accelerometer:didAccelerate: but recently that method has been deprecated in iOS 5.0. So what is the alternative way to get the accelerometer data? The documentation does not mention the alternative we are supposed to use.


回答1:


You should use the Core Motion framework (introduced in iOS 4.0) as a substitue. Create an instance of CMMotionManager and tell it to startAccelerometerUpdatesToQueue:withHandler:, passing it an NSOperationQueue and a block that will be executed on the specified queue whenever new accelerometer data is available.




回答2:


It seems that UIAccelerometer and UIAccelerometerDelegate were replaced by the CoreMotion framework.

You can find the answer here:

Why is accelerometer:didAccelerate: deprecated in IOS5?

I Hope it helps.




回答3:


Here is a useful sample code I found for CoreMotion from this link.

    @interface ViewController ()

    @property (nonatomic, strong) CMMotionManager *motionManager; 
    @property (nonatomic, strong) IBOutlet UILabel *xAxis; 
    @property (nonatomic, strong) IBOutlet UILabel *yAxis; 
    @property (nonatomic, strong) IBOutlet UILabel *zAxis; 

    @end

    @implementation ViewController
    - (void)viewDidLoad 
    { 
      [super viewDidLoad];

      self.motionManager = [[CMMotionManager alloc] init]; 
      self.motionManager.accelerometerUpdateInterval = 1;

      if ([self.motionManager isAccelerometerAvailable]) 
      { 
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{ 
              self.xAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.x]; 
              self.yAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.y]; 
              self.zAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.z];
            });
          }]; 
      } else 
      NSLog(@"not active"); 
    }
@end



回答4:


Its replaced by CoreMotion. See Motion Events.




回答5:


Add CoreMotion framework to the project first. Then:

#import <CoreMotion/CoreMotion.h>

@property (strong, nonatomic) CMMotionManager *motionManager;

- (void)viewDidLoad {
    _motionManager = [CMMotionManager new];
    _motionManager.accelerometerUpdateInterval = 0.01;       // 0.01 = 1s/100 = 100Hz
    if ([_motionManager isAccelerometerAvailable])
    {
        NSOperationQueue *queue = [NSOperationQueue new];
        [_motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error){
             NSLog(@"X = %0.4f, Y = %.04f, Z = %.04f",
                   accelerometerData.acceleration.x,
                   accelerometerData.acceleration.y,
                   accelerometerData.acceleration.z);
         }];
    }
}


来源:https://stackoverflow.com/questions/10171704/how-to-get-accelerometer-data-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!