iphone-CGMotion在横向模式下移动UIImageView

iphone-CGMotion在横向模式下移动UIImageView,iphone,objective-c,uiimageview,accelerometer,core-motion,Iphone,Objective C,Uiimageview,Accelerometer,Core Motion,我的应用程序很简单:我想用加速计(现在是Core Motion)在iPad的视图周围移动一个小的uiimageview。因此,我可以用加速计移动uiimageview,但在横向模式下建立工作边界时遇到问题。这是我的代码。问题是uiimageview倾向于粘附在边界上,而一些边界并不完全位于视图的边缘。这是我的代码,非常感谢您的帮助: - (void)startMyMotionDetect { __block float stepMoveFactor = 15; [self.motionMan

我的应用程序很简单:我想用加速计(现在是Core Motion)在iPad的视图周围移动一个小的uiimageview。因此,我可以用加速计移动uiimageview,但在横向模式下建立工作边界时遇到问题。这是我的代码。问题是uiimageview倾向于粘附在边界上,而一些边界并不完全位于视图的边缘。这是我的代码,非常感谢您的帮助:

- (void)startMyMotionDetect
{

__block float stepMoveFactor = 15;

[self.motionManager
 startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
 withHandler:^(CMAccelerometerData *data, NSError *error)
 {

 dispatch_async(dispatch_get_main_queue(),
                ^{

                    CGRect rect = self.sumbarine.frame;

                    float movetoY = rect.origin.x + (data.acceleration.x * stepMoveFactor);
                    float maxY = self.view.frame.size.width-rect.size.width;

                    float movetoX = (rect.origin.y + rect.size.height)
                    - (data.acceleration.y * stepMoveFactor);

                    float maxX = self.view.frame.size.height;

                    if ( movetoX >0 && movetoX < maxX ) {
                        rect.origin.x += (data.acceleration.y * stepMoveFactor);
                    };

                    if ( movetoY > 0 && movetoY < maxY ) {
                        rect.origin.y += (data.acceleration.x * stepMoveFactor);
                    };

                    [UIView animateWithDuration:0 delay:0
                                        options:UIViewAnimationOptionCurveEaseIn
                                     animations:
                     ^{
                         self.sumbarine.frame = rect;
                     }
                                     completion:nil
                     ];

                }
                );
 }
 ];
}
-(无效)启动MyMotionDetect
{
__块浮动系数=15;
[self.motionManager]
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc]init]
withHandler:^(CMAccelerMeterData*数据,N错误*错误)
{
dispatch\u async(dispatch\u get\u main\u queue(),
^{
CGRect rect=self.sumbarine.frame;
float movetoY=rect.origin.x+(data.acceleration.x*stepMoveFactor);
float maxY=self.view.frame.size.width-rect.size.width;
float movetoX=(rect.origin.y+rect.size.height)
-(数据.加速度.y*阶跃移动系数);
float maxX=self.view.frame.size.height;
if(movetoX>0&&movetoX0&&movetoY
卡滞问题来自您的移动条件。现在,你说,如果“moveToX/Y”大于0,并且小于宽度/高度,那么添加加速度X/Y

如果原点==或>大于“maxX/Y”,会发生什么情况?答:没有。因此,潜艇图像的行为就像它粘在一块木板上。也许它可以在另一个轴上移动,直到它最终被卡在无法移动的状态

你会想玩弄你的条件规则。我不知道你想在游戏中实现什么类型的操作/输入,所以我不能告诉你下一步该怎么做。我假设潜艇应该根据用户的手势/动作向上/向下移动

最后一条建议。加速度计基于重力。。。这对你们来说意味着,即使当你们向左移动时,某个轴上的加速度是正的。。。当你需要施加停止所需的力时,还有一条反向曲线。尝试查看motiongraphs应用程序(可在文档中找到),以帮助您更好地了解这些内容

记住,玩得开心