[英]Replace sharedAccelerometer with motionManager iOS
I've tried several method but I cannot find a working method to replace my 我尝试了几种方法,但是找不到替代我的方法
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
most recently i've tried to replace that with 最近,我试图用
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = (1.0 / kAccelerometerFrequency);
But it does not seem to work with my void 但这似乎不适用于我的虚无
- (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];
}
So what do I replace the sharedAccelerometer with since it was depreciated in iOS 5 那么,由于iOS 5中已贬值了sharedAccelerometer,我该如何替换呢?
There is no delegate message with motion manager. 运动管理器没有委托消息。 Use a timer (NSTimer) and poll the motion manager for its values, at intervals.
使用计时器(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];
Now pollAccel:
is called repeatedly. 现在
pollAccel:
被反复调用。 Pull out the accelerometer info: 拉出加速度计信息:
CMAccelerometerData* dat = self.motman.accelerometerData;
CMAcceleration acc = dat.acceleration;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.