如何在IOS中获取加速度计数据?

时间:2012-04-16 09:32:33

标签: ios accelerometer uiaccelerometer

我正在使用UIAccelerotmeterDelegate方法accelerometer:didAccelerate:,但最近该方法已在iOS 5.0中弃用。那么获取加速度计数据的另一种方法是什么?文档没有提到我们应该使用的替代方案。

6 个答案:

答案 0 :(得分:13)

您应该使用Core Motion framework(在iOS 4.0中引入)作为替代品。创建CMMotionManager的实例并将其告诉startAccelerometerUpdatesToQueue:withHandler:,并在新的加速度计数据可用时向其传递NSOperationQueue和将在指定队列上执行的块。

答案 1 :(得分:5)

似乎UIAccelerometer和UIAccelerometerDelegate被CoreMotion框架取代。

你可以在这里找到答案:

Why is accelerometer:didAccelerate: deprecated in IOS5?

我希望它有所帮助。

答案 2 :(得分:4)

以下是我从this链接找到的CoreMotion的有用示例代码。

    @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 :(得分:3)

CoreMotion取代。请参阅Motion Events

答案 4 :(得分:1)

首先将CoreMotion框架添加到项目中。然后:

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

答案 5 :(得分:0)

我正在为Swift 5.x回答这个问题。

@available(iOS, introduced: 2.0, deprecated: 13.0, message: "UIAcceleration has been replaced by the CoreMotion framework") public protocol UIAccelerometerDelegate : NSObjectProtocol { }

不符合UIAccelerometerDelegate,仅添加CoreMotion框架。

import CoreMotion

如果要显示动画或创建游戏,可以从以下代码开始。

self.motionManager = CMMotionManager()
self.motionManager?.startAccelerometerUpdates()
if motionManager.isAccelerometerAvailable {          
  motionManager.startAccelerometerUpdates(to: .main) { (data, error) in
    if let myData = data {
        let x =  String(format: "%.3f",myData.acceleration.x)
        let y =  String(format: "%.3f",myData.acceleration.y)
        let z =  String(format: "%.3f",myData.acceleration.z)
    DispatchQueue.main.async { 
          var rect = self.anyObject.frame
         let movetoX = rect.origin.x + CGFloat(myData.acceleration.x * stepMoveFactor)
         let movetoY = rect.origin.y - CGFloat(myData.acceleration.y * stepMoveFactor)
         if ( movetoX > 0 && movetoX < SCREEN_WIDTH ) {
            rect.origin.x += CGFloat(myData.acceleration.x * stepMoveFactor)
         }
         if ( movetoY > 1 && movetoY < maxY ) {
           rect.origin.y -= CGFloat(myData.acceleration.y * stepMoveFactor) }
         }
    }
 }

}

根据需要使用UIView.animate。此方法启动一组动画以在视图上执行。 animations参数中的块对象包含用于对一个或多个视图的属性进行动画处理的代码。