Вопрос по ios – Как получить данные акселерометра в IOS?

11

Я используюUIAccelerotmeterDelegate методaccelerometer:didAccelerate: но недавно этот метод устарел в iOS 5.0. Так какой же альтернативный способ получить данные акселерометра? В документации не упоминается альтернатива, которую мы должны использовать.

Ваш Ответ

5   ответов
13

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededCMMotionManagerError: User Rate Limit ExceededstartAccelerometerUpdatesToQueue:withHandler:Error: User Rate Limit ExceededNSOperationQueueError: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

#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);
         }];
    }
}
4

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

    @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
3

Error: User Rate Limit ExceededCoreMotionError: User Rate Limit ExceededError: User Rate Limit Exceeded.

5

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы