我明白了 0 0 0 0 0 0 0 0 0 0 0 0
- (void)applicationDidFinishLaunching:(UIApplication *)application {resultValues.text = @"";
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / kUpdateFrequency];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
printf("\n%d", x);
printf("\n%d", y);
printf("\n%d", z);
}
答案 0 :(得分:5)
问题在于你的printf语句。
UIAcceleration
类定义了它的属性,如下所示:
@property(nonatomic, readonly) UIAccelerationValue x
UIAccelerationValue是typedef
,如下所示:
typedef double UIAccelerationValue
d
printf说明符用于整数。你想要一个双打。你想要这样的东西:
printf("\n%g %g %g", acceleration.x, acceleration.y, acceleration.z);
(您也可以使用f
或e
。请参阅:http://en.wikipedia.org/wiki/Printf#printf_format_placeholders)
答案 1 :(得分:2)
加速度值是双倍的,并且以地球重力为单位进一步表示。例如1g中。毫不奇怪,当你将它们分配给整数并打印它们低于零的值时,因为加速度矢量的分量将分布在xyz轴上,除非你把它们中的一个精确地平行于地球的加速度矢量。
将它们视为它们的浮点值,您应该在操作设备时看到更改。
加速度计唯一一次给出合法的全零值是你自由落体时。
答案 2 :(得分:1)
x,y,z是浮点数,而不是整数。
将printf("\n%d", x);
更改为printf("\n%f", x);
答案 3 :(得分:1)
printf("\n%.5f", x);
printf("\n%.5f", y);
printf("\n%.5f", z);