我正在制作一个 iphone 应用程序,其中一个球将根据用户倾斜设备的方式在屏幕上滚动。如果设备平放在桌子上,理论上球不会移动。如果设备完全向上倾斜,我希望球以最大速度直接向下滚动。速度取决于设备倾斜到平面位置的距离。此外,它也适用于用户向右或向左或向上或四者组合的情况。我现在正在使用加速度计,球在移动,它工作正常,我只是对物理学不太熟悉。如果有人对如何使它顺利工作有任何建议,请告诉我。
谢谢!
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float xx = -[acceleration x];
float yy = [acceleration y];
float z = -[acceleration z];
z = 1 - z;
NSString * zaxis = [NSString stringWithFormat:@"%f", z];
lblz.text = zaxis;
lbly.text = [NSString stringWithFormat:@"%f", yy];
lblx.text = [NSString stringWithFormat:@"%f", xx];
CGFloat newx;
CGFloat newy;
if (yy > 0)
{
newy = ball.center.y - ((1 - yy) * z);
}
else
{
newy = ball.center.y + ((1 - yy) * z);
}
if (xx > 0)
{
newx = ball.center.x - ((1 - xx) * z);
}
else
{
newx = ball.center.x + ((1 - xx) * z);
}
CGPoint newPoint = CGPointMake(newx, newy);
ball.center = newPoint;