0

如果我们倾斜设备,我正在尝试更改 UILabel 值,因为我正在尝试实现 CMMotionManager 但它对我不起作用,这是我正在使用的代码,

我在 lib 中添加了 CoreMotion。

#import <CoreMotion/CoreMotion.h>

 CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    motionManager.accelerometerUpdateInterval = .2;

    [motionManager startAccelerometerUpdates];

    if ([motionManager isAccelerometerAvailable] == YES)
    {

        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:1];

        [motionManager startAccelerometerUpdatesToQueue:queue  withHandler:^(CMAccelerometerData *data, NSError *error)
         {

             CMAcceleration acceleration = data.acceleration;


             if(acceleration.y < -0.25) { // tilting the device to the right

                 [self setTextforlabel:@"right"];

             } else if (acceleration.y > 0.25) { // tilting the device to the left

                                  [self setTextforlabel:@"left"];
             }
         }];
    }

这里有什么问题,isAccelerometerAvailable总是错误的

startAccelerometerUpdatesToQueue它不会开火。

任何人都可以帮助我做这个或有任何其他方式来做倾斜设备。

4

1 回答 1

0

您是在设备上还是在刺激器中运行它?加速度计在刺激器中不起作用

于 2014-11-05T07:27:26.530 回答