我尝试了几种方法,但我找不到替换我的
的工作方法[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
最近我试图用
替换它motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = (1.0 / kAccelerometerFrequency);
但它似乎不适合我的虚空
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
CGPoint pt = theCar.center;
CGFloat accel = acceleration.x * kAccelerationSpeed;
float halfCarWidth = theCar.frame.size.width / 2;
if(pt.x - halfCarWidth + accel > 0 && pt.x + halfCarWidth + accel < 320) {
pt.x += accel;
}
[theCar setCenter:pt];
}
那么我该如何替换sharedAccelerometer,因为它在iOS 5中被折旧
答案 0 :(得分:1)
动作管理器没有委托消息。使用计时器(NSTimer)并定期轮询运动管理器的值。
self.motman = [CMMotionManager new];
if (!self.motman.accelerometerAvailable) {
// report error or whatever
return;
}
self.motman.accelerometerUpdateInterval = 1.0 / 30.0;
[self.motman startAccelerometerUpdates];
self.timer =
[NSTimer
scheduledTimerWithTimeInterval:self.motman.accelerometerUpdateInterval
target:self selector:@selector(pollAccel:) userInfo:nil repeats:YES];
现在重复调用pollAccel:
。拉出加速度计信息:
CMAccelerometerData* dat = self.motman.accelerometerData;
CMAcceleration acc = dat.acceleration;