如果我们倾斜设备,我正在尝试更改 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
它不会开火。
任何人都可以帮助我做这个或有任何其他方式来做倾斜设备。