Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何根据倾斜量更新x和y位置?_Ios_Objective C_Core Motion - Fatal编程技术网

Ios 如何根据倾斜量更新x和y位置?

Ios 如何根据倾斜量更新x和y位置?,ios,objective-c,core-motion,Ios,Objective C,Core Motion,如何根据倾斜量更新对象的x和y位置 我正在尝试根据倾斜运动量更新我的\u bg对象的x和y位置 此外,如果将设备放在桌子上,位置应回到其原始位置 我正在尝试这样做: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _motionManager = [[CMMotionManager all

如何根据倾斜量更新对象的x和y位置

我正在尝试根据倾斜运动量更新我的\u bg对象的x和y位置

此外,如果将设备放在桌子上,位置应回到其原始位置

我正在尝试这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _motionManager = [[CMMotionManager alloc] init];

    [_motionManager startGyroUpdates];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1/30
                                              target:self
                                            selector:@selector(updateGyro)
                                            userInfo:nil repeats:YES];
}



- (void)updateFromGyro
{
    self.x += _motionManager.gyroData.rotationRate.x;
    self.y += _motionManager.gyroData.rotationRate.y;

    _bg.center = CGPointMake(self.x, self.y);
}
问题是,物体永远不会停止移动


谢谢大家!

这可能会有帮助。但根据问题提供的有限数据,不确定。您可能还应该切换到使用绝对位置/旋转,而不是帧之间的相对更改

只需设置一个最小阈值。这将防止分钟移动显示为更新:

if( _motionManager.gyroData.rotationRate.x > ROTATION_MIN )
{
    self.x += _motionManager.gyroData.rotationRate.x;
}
if( _motionManager.gyroData.rotationRate.y > ROTATION_MIN )
{
    self.y += _motionManager.gyroData.rotationRate.y;
}
_bg.center = CGPointMake(self.x, self.y);

速率是单位时间内的变化量。因此,您设置的坐标是相对于设备移动的速度,而不是其实际偏移量。您可能需要查看它的
姿态
(它与任意参考系的实际偏移)。文档是。

我认为您在设置新中心时犯了错误。试试这个:

- (void)updateFromGyro
{
    self.x = _bg.center.x + _motionManager.gyroData.rotationRate.x;
    self.y = _bg.center.y + _motionManager.gyroData.rotationRate.y;

    _bg.center = CGPointMake(self.x, self.y);
}

顺便说一下,即使您将设备放在工作台上,应用程序也会不断接收陀螺更新,因为工作台的坡度不能保证为0度。

这对于“放在工作台上”时的重置非常有用。我不确定这是否能改变他的背景。问题是,即使倾斜,它也会弹回到原来的位置。谢谢!这不是我想要的,但实际上我需要它(谢谢你,先生,为我指明了正确的方向!这种态度正是我所需要的