我試圖用CMDeviceMotion跟蹤iPhone向後傾斜,然後做些事情。我已經成功創建了CMMotionManager,我從系統獲取運動數據,並且在超過特定加速度的情況下篩選結果。使用CMDeviceMotion跟蹤iPhone傾斜隨着時間的推移
我想做的事情是當設備向後或向前傾斜超過特定速度時檢測到。我怎麼做?
這裏是我到目前爲止的代碼:
更新:我想我解決了這個問題。我正在尋找rotationRate屬性CMRotationRate。我已經將它們配對在一起,我只需要x值,所以我會繼續努力。如果有人在下面的代碼中有一些提示,非常感謝。
- (void)startMotionManager{
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
motionManager.deviceMotionUpdateInterval = 1/15.0;
if (motionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
//CMAttitude *attitude = motion.attitude;
//NSLog(@"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
//[motionManager startDeviceMotionUpdates];
} else {
NSLog(@"No device motion on device.");
[self setMotionManager:nil];
}
}
- (void)handleDeviceMotion:(CMDeviceMotion*)motion{
CMAttitude *attitude = motion.attitude;
float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
CMAcceleration userAcceleration = motion.userAcceleration;
float rotationRateThreshold = 7.0;
CMRotationRate rotationRate = motion.rotationRate;
if ((rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(@"motion.rotationRate = %f", rotationRate.x);
[self showMenuAnimated:YES];
}
}
else if ((-rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(@"motion.rotationRate = %f", rotationRate.x);
[self dismissMenuAnimated:YES];
}
}
}
這是我需要幫助的數學.. – 2011-04-17 15:41:29
啊對不起,沒有看到足夠的代碼,編輯我的答案... – 2011-04-17 16:47:13
我更加感興趣的加速度和旋轉速度,以檢測設備'翻轉'倒退」。當他們翻轉它時,想想一個警察和他的徽章(如果他們仍然這樣做的話)。我想我已經解決了它的大部分。我會編輯我的OP。 – 2011-04-17 16:59:15