iOS 在 iPad 上触摸(自动)偏移

发布于 2024-12-11 07:00:15 字数 397 浏览 0 评论 0原文

几乎没有关于这方面的信息,所以这是我的问题:

我正在开发一款 2 名玩家“面对面坐着”的 iPad 游戏,而另一侧的玩家很难点击按钮......因为苹果似乎会根据设备方向自动移动触摸(这对于常规使用或主要播放器来说非常有用)。有什么方法可以重置它或告诉ipad不要这样做吗? (顺便说一句,iPhone 也这样做)

这就是我发现的全部: 鉴于这种明显的不一致,我决定通过打开移动部件的“边界”来欺骗它,以便用户在部件区域下方有一个可以点击的区域。总的来说,用户似乎是击中某个对象的下方而不是上方 - 因此不太会注意到 iOS 引起的移动。

因此,如果没有简单的方法来防止偏移,那么为对方玩家移动每个 uiview(或子类)边界的唯一解决方案?或者这是正确的吗?

任何答案表示赞赏。

There is almost no information out there about this, so here is my problem:

I'm working on a 2 player 'sit across from eachother' ipad game and the player who is on the opposite side has a hard time tapping buttons... because it appears that Apple auto-shifts the touch based on the device orientation (which is great for regular use or the main player). Is there any way to reset it or tell the ipad to not do that? (Btw iPhone does this too)

This is all I have found:
Given this apparent inconsistency, I've decided to just cheat it by opening up the moving parts' 'bounds' so that there is an area for the user to hit below the part's area. On the whole, users seem to hit below an object rather than above it - so don't notice the shifting caused by iOS as much.

So if there is no easy way to prevent the offsetting, is the only solution to shift each uiview's (or subclass's) bounds for the opposite player? Or is that even correct?

Any answers appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十雾 2024-12-18 07:00:15

是的,触摸偏移确实存在,其大小没有记录,并且无法取消(至少使用公共 API)。

您必须对偏移量本身进行逆向工程。我只会在 UIKit 表示触摸的地方显示一个小圆圈,然后通过反复试验来调整它。当您拥有偏移量时,您可以仅在对方玩家的事件处理程序中手动应用它(-2 * 偏移量)。

我无法判断它的感觉有多正确,你必须进行一些尝试。

Yes, the touch offset does exist, its size is not documented and it cannot be cancelled (at least, with public API).

You have to reverse-engineer the offset itself. I would just display a small circle where UIKit says the touch is, and then adjust it by trial and error. When you have the offset, you can manually apply it (-2 * offset) in the event handlers for the opposite player only.

I can't tell how right it will feel, you have to experiment a bit.

千と千尋 2024-12-18 07:00:15

编辑:天哪,你们我终于找到了方法!我创建了 UIView 的子类,并让我的游戏视图(其中包含所有 UIScrollViews、UIButtons 等)使用该子类。然后这是我添加到子类中的唯一代码:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint touchSpot = point;
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"] && touchSpot.y < 512)
    {
        touchSpot = CGPointMake(touchSpot.x, touchSpot.y+10);
    }

    UIView *hitView = [super hitTest:touchSpot withEvent:event];

    if (hitView == self)
        return self; // you can return nil here if you want touches to go through this view (ie transparent space in a HUD overlay or w/e)

    return hitView;
}

到目前为止,效果很好......坐在另一端的人具有良好的触摸响应,就像“主”侧的玩家一样。不过,请注意在此视图之上呈现视图...一切都将通过该方法...

旧解决方案(在我发现之前^):

好吧,现在我的解决方案是子类化uigesturerecognizer,转发任何触摸(即对识别器没有限制),然后将其添加到我的游戏视图中。然后,响应任何触摸的方法仅在状态为 Ended 时运行,如果是,则它会继续循环遍历每个子视图(不多)以查看触摸是否与子视图相交。同样,对于相反的玩家,它会比较稍微偏移的矩形(对于每个子视图)。

我会看看是否能找到更有效的方法...但到目前为止还可以。

我也可以将我的游戏视图设为 uiview 子类,并覆盖touchesbegan/moved/end,但我已经将其全部放置出来,所以我使用了识别器(因此不必子类化 uiview)。

EDIT: Omg you guys I finally found a way! I made a subclass of UIView, and made my game view (which houses all of the UIScrollViews, UIButtons, etc) use that subclass. And then this was the only code I added to the subclass:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint touchSpot = point;
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"] && touchSpot.y < 512)
    {
        touchSpot = CGPointMake(touchSpot.x, touchSpot.y+10);
    }

    UIView *hitView = [super hitTest:touchSpot withEvent:event];

    if (hitView == self)
        return self; // you can return nil here if you want touches to go through this view (ie transparent space in a HUD overlay or w/e)

    return hitView;
}

So far this has worked nicely... the person sitting on the opposite end has good touch response just like the player on the "home" side. Beware of presenting views on top of this view though... everything will go through that method...

Old solution (before I found that out ^):

Well, right now my solution has been to subclass uigesturerecognizer, forward ANY touch (ie no constraints on recognizer), then add it to my game's view. Then the method that responds to any touch only runs when the state is Ended, and if so, it then proceeds to loop through every subview (not many) to see if the touch intersected one. As well, for the opposite player it compares the rect offsetted a bit (for each of the subviews).

I'll see if I can find a more efficient way... but so far okay.

I could have also made my game-view a uiview subclass and overridden touchesbegan/moved/ended but I already had it all placed out, so I went with the recognizer (thus not having to subclass uiview).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文