如何将校准代码应用于实际的加速度计方法?
经过一两次谷歌之后,我想出了这段代码来保存用户当前所在的位置并将其保存到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论