gpt4 book ai didi

iphone - 使用 CMDeviceMotion 跟踪 iPhone 随着时间的推移的倾斜度

转载 作者:行者123 更新时间:2023-12-03 21:07:21 25 4
gpt4 key购买 nike

我正在尝试使用 CMDeviceMotion 来跟踪 iPhone 何时向后倾斜,然后执行某些操作。我已成功创建 CMMotionManager,正在从系统获取运动数据,并且正在过滤高于特定加速度的结果。

我想做的是检测设备何时向后或向前倾斜超过一定速度。我该怎么做?

这是我到目前为止的代码:

更新:我想我已经解决了。我正在寻找旋转速率属性,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];
}
}
}

最佳答案

你看过CMAttitude吗? ?听起来像是您所需要的,我猜还需要一些数学知识。

<小时/>

编辑:好的,你做到了。

引用Apple documentation ,在“获取当前设备方向”一章中:

If you need to know only the general orientation of the device, and not the exact vector of orientation, you should use the methods of the UIDevice class to retrieve that information. Using the UIDevice interface is simple and does not require that you calculate the orientation vector yourself.

太棒了,他们为我们做了数学计算。然后我想像上面的文档所解释的那样,像你一样跟踪加速度+跟踪方向,应该可以使用 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown 来完成这项工作?

如果没有一个 UIDeviceOrientation 满足您的需求,您需要从两个 CMAttitude 引用计算一些空间角度,它提供了一个 rotationMatrix 和一个四元数...这些学校数学对我来说很遥远...然后我建议也许用这些搜索/问一个仅数学问题。

关于iphone - 使用 CMDeviceMotion 跟踪 iPhone 随着时间的推移的倾斜度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com