开发者

Finding the irregularly-shaped subview that contains a point

开发者 https://www.devze.com 2023-04-11 06:34 出处:网络
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.

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 simp开发者_C百科le, but not necessarily convex. Also, the most precise hit-test I use is with Quartz routine CGPathContainsPoint

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号