iOS 7 中手势识别的问题 [英] Problems with gesture recognizer in iOS 7

查看:22
本文介绍了iOS 7 中手势识别的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向屏幕添加几个 UIView 对象(例如 5),一个在另一个中.例如,view5.superview = view4view4.superview = view3view3.superview=view2view2.superview= 视图 1.对于所有这些 UIView 我设置了 uitapgesturerecognizer;对于 view1-4,我只是在回调中执行 NSLog(@"tap %@", self),而对于 view5 tap 我设置以下内容:从层次结构中删除 view4,然后将相同的对象 view4' 放在层次结构的同一位置.该对象还包含设置了 UITapGestureRecognizer 的 view5'(实际上,我用类似的标记替换了一部分).

然后我开始点击view5.一段时间后,view5 继续捕捉它的点击并且一切正常,但是之后随机点击次数(每次这个数字不同时)其中一个 view1-4 对象开始捕捉这个点击,尽管我们仍在点击 view5.整个问题具有随机性——有时发生在第 10 次发射,有时发生在第二次发射.有时错误的对象会在第一次点击时开始捕捉点击.此外,当一切都出错时,我永远不知道什么物体会被轻拍.view(n+1) 的框架被设置为例如框架 view(n) 的一半,而 view1 的框架 - 例如(0,0 320, 460).

上述 ui 对象的所有操作都是在主线程中进行的,我所讲述的一切都在 iOS 4.3 - 6.1 上完美运行,并提供了更复杂的示例.但是 iOS7 让它变成了某种随机地狱.

更新:我创建了一个示例项目,以简化调试过程.没有点击添加/删除子视图操作.屏幕上只有 4 个视图,点击应用程序会记录点击的视图.因此,您需要点击最小视图 (4).如果您在日志中看到tap 4 tap 4 tap 4..." - 这就是一切正常时的情况,请停止并再次运行,再次停止并运行,再次停止并运行等.在某些运行中(可能在 10+ 成功运行)你不会在第一行看到tap 4",你会看到tap 1"或tap 2"或tap 3",它会继续这样 - 这些都是坏情况.

示例项目可以从这里下载:http://tech.octopod.com/test/BuggySample.zip(存档中只有 33 Kb).

更新 2

我们已经向 Apple 发布了一个错误,当我们得到一些反馈时,我会在这里发布.但是,任何好的解决方法将不胜感激!

更新 3

由 Yuvrajsinh 提供的解决方案实际上正在处理示例项目.不幸的是,它仍然无助于解决最初出现在主项目中的问题.现在的主要原因是,如果任何没有自手势的视图放在可点击的内容上,它下面的随机视图元素开始捕捉交互(而不是顶部有交互手势设置的元素.你有什么想法可以解决吗?更新的样本可以从这里下载:http://tech.octopod.com/test/BuggySample2.zip

解决方案

由于该问题仅在 iOS 7 中出现,您可以使用其中一种新的委托方法来解决该问题:

–gestureRecognizer:shouldRequireFailureOfGestureRecognizer:–gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:

我通过实现 gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer 并爬行"手势视图的超级视图来解决它,因此如果我发现超级视图的手势等于提供的手势,我可以返回是".我在这里详细说明了我的完整解决方案:https://stackoverflow.com/a/19659848/1147934.

说明
iOS 7 中手势识别器的问题在于,超级视图的手势在其子视图手势之一接收触摸之前接收它的触摸.这会导致超级视图手势识别然后取消子视图的识别器......这是(不正确?)并且已经向Apple提交了多个错误.有人指出,Apple 不保证手势接收触摸的顺序.我认为很多我们"一直依赖于 iOS 7 中更改的实现细节.这就是我们使用新的委托方法的原因,这些方法似乎旨在帮助我们解决这个问题.

注意:我使用自己的子类识别器进行了广泛的测试,记录了所有触摸,发现识别器失败的原因是因为父视图手势在子视图手势约 5% 之前接收触摸的情况.每次发生这种情况时,都会发生故障.如果您有很多手势的深"层次结构,这种情况会更频繁地发生.

新的委托方法可能会令人困惑,因此您需要仔细阅读它们.

我正在使用该方法(我重命名了参数以使其更易于理解)

–gestureRecognizer:(UIGestureRecognizer *)thisRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *) otherRecognizer.

如果您返回YES",则提供的手势识别器 otherRecognizer 将要求 thisRecognizer 失败才能被识别.这就是为什么在我的回答中,我爬上超级视图层次结构以检查它是否包含具有 otherRecognizer 的超级视图.如果是这样,我希望 otherRecognizer 要求 thisRecognizer 失败,因为 thisRecognizer 在子视图中并且 应该 失败之前它的超级视图的手势被识别.这将确保子视图手势在其父视图的手势被识别之前被识别.有道理吗?

替代方案
我可以反过来使用:

–gestureRecognizer:(UIGestureRecognizer *)thisRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherRecognizer

现在我需要遍历我的整个subview层次结构,检查otherRecognizer是否在其中,如果是,则返回YES.我不使用这种方法,因为爬取整个子视图层次结构比检查超级视图层次结构要困难得多,而且成本也高.抓取子视图层次结构必须是一个递归函数,而我可以使用一个简单的 while 循环来检查超级视图的层次结构.所以我推荐我概述的第一种方法.

警告!
使用 gestureRecognizer:shouldReceiveTouch: 时要小心.问题是哪个手势首先接收触摸的问题(取消另一个手势)……这是解决冲突的问题.如果你实现了gestureRecognizer:shouldReceiveTouch:,如果子视图手势失败,你就有可能拒绝父视图的手势,因为你必须猜测子视图手势可能被认可.子视图手势可能会因触摸越界以外的原因而合法失败,因此您必须了解实现细节才能正确猜测.您希望在子视图手势失败时能够识别超级视图手势,但无论如何您都无法确定它是否会在实际失败之前确定.如果子视图手势失败,通常您希望超级视图手势能够识别.这是正常响应者链(子视图超级视图),如果你弄乱了它,最终可能会出现意外行为.

I'm adding several UIView objects (e.g. 5) to the screen, one inside the other. This, for example, view5.superview = view4, view4.superview = view3, view3.superview=view2, view2.superview = view1. For all these UIView I set uitapgesturerecognizer; for view1-4 I just do NSLog(@"tap %@", self) in callback, while for view5 tap I set the following: delete view4 from the hierarchy, then put the same object view4' at the same place of the hierarchy. This object also contains view5' for which UITapGestureRecognizer is set (practically, I replace one part of markup with similar one).

Then I start clicking on view5. Some time view5 keeps catching its tap and everything's OK, but random number of taps later (every time this number is different) one of the view1-4 objects starts catching this tap, though we're still clicking the view5. The whole problem has a random character - sometimes it occurs at the 10th launch, sometimes at the second. Sometimes wrong objects start catching taps at the first tap. Also I never know what object will catch a tap, when everything goes wrong. The frame for view(n+1) was set, e.g., as a half of the frame view(n), while the frame for view1 - e.g. (0,0 320, 460).

All operations with ui objects described above are conducted in the main thread, and everything I've told about perfectly worked on iOS 4.3 - 6.1 with much more complex examples. But the iOS7 makes out of it some kind of a random hell.

Update: I've created a sample project, to simplify the debug process. No add/remove subview operations on tap. Only 4 views on screen, on tap the app logs what view was tapped. So, you need to tap on smallest view (4). If you see "tap 4 tap 4 tap 4…" in the log - this is the case when everything works fine, stop and run again, stop and run again, stop and run again, etc. And at some runs (maybe after 10+ successful runs) you won't see "tap 4" on the first line, you will see "tap 1" or "tap 2" or "tap 3", and it will continue so - these are the bad cases.

Sample project can be downloaded from here: http://tech.octopod.com/test/BuggySample.zip (just 33 Kb in archive).

Update 2

We've posted a bug to Apple, I'll post here when we will get some feedback. However, any good workaround would be much appreciated!

Update 3

Solution, provided by Yuvrajsinh is really working on the sample project. Unfortunately, it still does not help to solve the problem that occurred in the main project where it initially appeared. The main reason for now is that if any view without self gesture is laying upon the clickable content, random view element under it starts catching the interaction (instead of the top one with interaction gesture set. Do you have any ideas how it can be solved? The updated sample can be downloaded from here: http://tech.octopod.com/test/BuggySample2.zip

解决方案

Because the problem is only occurring in iOS 7, you can use one of the new delegate methods to resolve the issue:

– gestureRecognizer:shouldRequireFailureOfGestureRecognizer:
– gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:

I resolved it by implementing gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer and "crawling" up the gesture's view's superview so I could return "YES" if I find the superview's gesture is equal to the one provided. I detail my full resolution here: https://stackoverflow.com/a/19659848/1147934.

Explanation
The problem with gesture recognizers in iOS 7 is that a superview's gesture is receiving its touches before one of its subview gestures receives its touches. This causes the superview gesture to recognize which then cancels out the sub view's recognizer... this is (incorrect?) and multiple bugs have been filed with Apple. It's been pointed out that Apple doesn't guarantee the order in which gestures receive touches. I think a lot of "us" have been relying on an implementation detail that changed in iOS 7. This is why we use the new delegate methods, which seem designed to help us address this problem.

Note: I did extensive testing by using my own sublcassed recognizers, logging all touches and discovered that the reason recognizers fail is because superview gestures were receiving touches before a subview's gesture was in about ~5% of the cases. Every time this happened, failure occurred. This does happen more often if you have "deep" hierarchies with lots of gestures.

The new delegate methods can be confusing, so you need to read them carefully.

I'm using the method (I've renamed the arguments to make them easier to understand)

– gestureRecognizer:(UIGestureRecognizer *)thisRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *) otherRecognizer.

If you return "YES", then the gesture recognizer provided, otherRecognizer, will require thisRecognizer to fail before it can be recognized. This is why, in my answer, I crawl up the superview hierarchy to check if it contains a superview that has the otherRecognizer. If it does, I want otherRecognizer to require thisRecognizer to fail because thisRecognizer is in a subview and should fail before it's superview's gesture is recognized. This will make sure that subview gestures are recognized before their superview's gestures are. Make sense?

Alternative
I could go about it the other way around and use:

– gestureRecognizer:(UIGestureRecognizer *)thisRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherRecognizer

Now I would need to crawl through my entire subview hierarchy, check if otherRecognizer is in it and return YES if it is. I don't use this method because crawling the entire subview hierarchy is much more difficult and expensive to do than to check a superview hierarchy. Crawling a subview hierarchy would have to be a recursive function, while I can use a simple while loop to check a superview's hierarchy. So I recommend the first approach I outline.

Warning!
Be careful about using gestureRecognizer:shouldReceiveTouch:. The issue is a problem of which gesture receives touches first (canceling the other gesture out)... it's a problem of conflict resolution. If you implement gestureRecognizer:shouldReceiveTouch:, you risk rejecting a superview's gesture if the subview gesture fails because you have to guess when a subview gesture might be recognized. A subview gesture may legitimately fail for reasons other than the touches are out of bounds, so you would have to know implementation details in order to guess correctly. You want the superview gesture to be recognized when the subview gesture fails but you don't really have anyway to know for certain if it will fail before it actually fails. If a subview gesture fails, normally you want the superview gesture to then recognize. This is the normal responder chain (subview superview) and if you mess with that you could end up with unexpected behavior.

这篇关于iOS 7 中手势识别的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆