如何强制 TouchesEnded

发布于 2024-12-14 04:50:16 字数 345 浏览 1 评论 0原文

用户将游戏块(第 i 个图像视图)拖动到其目标位置。程序进行计数,发现所有项目都位于正确的位置,宣布“游戏结束”并将 userInteractionEnabled 设置为 NO。

很好,只是如果用户的手指仍然放在游戏棋子上,用户可能会意外地将棋子拖回目标区域之外。正在显示“游戏结束”,但该棋子不再位于正确的位置。

有没有办法强制touchesEnded(不检测touchesEnded),以便当棋子到达最终目的地时(有效)破坏与游戏棋子的接触(即,这样用户就不会意外地将其拉出位置) )?

userInteractionEnabled = NO 似乎要在触摸释放后才生效。

The user drags the game piece (the i-th image view) to its target location. The program counts, sees that all the items are in the correct places, announces "Game Over" and sets userInteractionEnabled to NO.

Great, except that if the user's finger is still down on the game piece, the user can drag the piece back out of the target area by accident. "Game Over" is showing but the piece is no longer in the correct place.

Is there a way to force a touchesEnded (not detect a touchesEnded) so that the contact with the game piece is (effectively) broken when the piece is in its final destination (i.e. so that the user can't accidentally pull it out of position)?

userInteractionEnabled = NO does not seem to take effect until the touch is released.

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

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

发布评论

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

评论(2

东北女汉子 2024-12-21 04:50:16

我不知道有什么方法可以强制touchesEnded。

显然,处理此问题的一种方法是让您的应用程序保持指示游戏结束的状态,并防止在该状态下移动任何游戏块。

您可以尝试 beginIgnoringInteractionEvents,尽管我怀疑这是否能满足您的要求。我真的认为管理应用程序中的状态可以确保您做正确的事情,而不是在达到最终状态后移动该部分,这是正确的方法。

来自 Apple 的 iOS 事件处理指南:

在一段时间内关闭触摸事件的传送。一个应用程序可以
调用 UIApplication 方法 beginIgnoringInteractionEvents 及更高版本
调用 endIgnoringInteractionEvents 方法。第一个方法停止
应用程序完全不接收触摸事件;第二个
方法被调用以恢复此类事件的接收。你有时
想要在代码执行时关闭事件传递
动画。

I'm not aware of a way to force touchesEnded.

Obviously one way to handle this is for your application to maintain state that indicates that the game is over and guard against moving any game pieces when in that state.

You might try beginIgnoringInteractionEvents, though I doubt this will do what you are looking for. I really think managing state in your application that ensures that you will do the right thing, not moving the piece once the end state has been reached, is the way to go.

From Apple's Event Handling Guide for iOS:

Turning off delivery of touch events for a period. An application can
call the UIApplication method beginIgnoringInteractionEvents and later
call the endIgnoringInteractionEvents method. The first method stops
the application from receiving touch events entirely; the second
method is called to resume the receipt of such events. You sometimes
want to turn off event delivery while your code is performing
animations.

淡水深流 2024-12-21 04:50:16

我想在自己的应用程序中执行非常类似的操作后发现了这个问题。我希望在单击移动操作后立即运行 touchesEnded: 方法(如 touchDown 操作方法),但不知道如何实现这一点。最后这就是我所做的并且有效! :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
...

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

  [self touchesEnded:touches withEvent:event];
 });
}

这很完美!

I found this question after wanting to do a very similar action within my own application. I wanted the touchesEnded: method to be run immediately after clicking on the moving action (like a touchDown action method), but did not know how to achieve this. In the end this is what I did and it WORKED! :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
...

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

  [self touchesEnded:touches withEvent:event];
 });
}

This worked perfectly!

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