如何将校准代码应用于实际的加速度计方法?

发布于 2024-12-05 10:02:52 字数 1429 浏览 0 评论 0原文

经过一两次谷歌之后,我想出了这段代码来保存用户当前所在的位置并将其保存到 NSUserDefaults 中。这是在我的设置视图中: 代码:

//Add code for calibrating accelerometer
        UIAcceleration *acceleration;
        float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
        float accelY = (acceleration.y - [[NSUserDefaults standardUserDefaults] floatForKey:@"Y-Calibrate"]);
        [[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"X-Calibrate"];
        [[NSUserDefaults standardUserDefaults] setFloat:accelY forKey:@"Y-Calibrate"];

要重置校准,我执行以下操作: 代码:

[[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"X-Calibrate"];
    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"Y-Calibrate"];

现在我需要一点帮助。现在我在 NSUserDefaults 中拥有了这些值。我如何将其应用于以下代码以便“完全”实现校准? 这是当前加速度计的代码: 代码:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceration {
    AccelPoint.x += (acceration.x*50);
    if (AccelPoint.x < 0) {
        AccelPoint.x = 320;
    }
    if (AccelPoint.x > 320) {
        AccelPoint.x = 0;
    }
    playerSpeed += 0.066;
    AccelPoint.y += playerSpeed;
    Square.center = AccelPoint; 
}

这是一些变量及其定义方式: 代码:

CGPoint AccelPoint;
float playerSpeed;

谢谢!

还有关于加速计代码的快速小网站问题,我应该使用 if-elseif 吗?这样做会更有效还是我应该把它留在 if-if 上?

After a google or two, I came up with this code to save the current position the user is in and save it to NSUserDefaults. This is in my settings view:
Code:

//Add code for calibrating accelerometer
        UIAcceleration *acceleration;
        float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
        float accelY = (acceleration.y - [[NSUserDefaults standardUserDefaults] floatForKey:@"Y-Calibrate"]);
        [[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"X-Calibrate"];
        [[NSUserDefaults standardUserDefaults] setFloat:accelY forKey:@"Y-Calibrate"];

To reset the calibration I do:
Code:

[[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"X-Calibrate"];
    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"Y-Calibrate"];

Now here is where I need a slight bit of help. Now that I have these values in NSUserDefaults. How would I apply it to the following code so that the calibration is 'fully' implemented?
Here is the code for the accelerometer currently:
Code:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceration {
    AccelPoint.x += (acceration.x*50);
    if (AccelPoint.x < 0) {
        AccelPoint.x = 320;
    }
    if (AccelPoint.x > 320) {
        AccelPoint.x = 0;
    }
    playerSpeed += 0.066;
    AccelPoint.y += playerSpeed;
    Square.center = AccelPoint; 
}

This is some of the variables and how they are defined:
Code:

CGPoint AccelPoint;
float playerSpeed;

Thanks!

Also quick little site question for that accelerometer code, should I be using if-elseif? Would that be more efficient or should I leave it at if-if?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文