查找包含点的不规则形状子视图
我的主视图有几个(也许很多)多边形活动区域作为子视图实现。当用户触摸视图时,我想确定她是否点击了这些活动区域之一。
有什么比简单的算法更好的方法吗?该算法会迭代所有[可能*]活动区域的列表,并询问每个区域该点是否在其中?
我无法使用(不覆盖它)hitTest:withEvent:
,因为它的文档指出“此方法会忽略隐藏的视图对象、禁用用户交互的视图对象或 alpha 级别小于 0.01 的视图对象。”。我的子视图确实是隐藏的(大多数时候)。
我可以看到这些选项:
实现我自己的成熟四叉树实现。这散发着过早优化的味道,这是重新发明轮子,因为我预计 Apple 的命中测试实现已经得到优化
通过维护每个最大和最小 x 和 y 坐标以及命中测试来限制 [可能*] 区域首先在那个矩形上。事实上,我的子视图框架就是那些矩形。我希望 Apple 的命中测试能够使用这些。
在我的子视图中覆盖 Cocoa Touch 的
hitTest:withEvent:
以删除记录的限制。希望这不会对我的应用程序产生不利影响(例如,我可以忽略子视图上的任何虚假触摸事件,或者将它们转发到我的主视图)。天真地相信用户永远不会在实际使用中添加太多的活动区域,从而使天真的算法表现太差。
有什么建议吗?
请注意,多边形总是简单的,但不一定是凸的。 另外,我使用的最精确的命中测试是使用 Quartz 例程 CGPathContainsPoint
My main view has several (perhaps many) polygonal active areas implemented as subviews. When the user touches the view, I want to identify if she hit one of those active areas.
Is there anything better than a naive algorithm that would iterate over the list of all [possible*] active areas and ask each of them whether the point is inside it?
I cannot use (without overriding it) hitTest:withEvent:
as its documentation states "This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.". And my subviews are indeed hidden (most of the time).
I can see these options:
Implement my own full-blown quad-tree implementation. This reeks of premature optimization, and this is reinventing the wheel as I expect Apple's hit test implementation to be optimized already
Restrict the [possible*] areas by maintaining for each its maximum and minimum x and y coordinates, and hit-testing on that rectangle first. In fact, my subviews
frame
are those rectangles. I expect Apple's hit-testing to use those.Override Cocoa Touch's
hitTest:withEvent:
in my subviews to remove the documented restriction. Hopefully, this won't have adverse effect on my app (I can ignore any spurious touch event on my subviews for example, or forward them to my main view).Be naive and trust that the users will never add too many active areas in real use for the naive algorithm to perform too badly.
Any suggestion?
Note that the polygons are always simple, but not necessarily convex.
Also, the most precise hit-test I use is with Quartz routine CGPathContainsPoint
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论