How to determine if iPad user taps within an irreg

2019-04-15 12:36发布

I've hooked up a UITapGestureRecognizer to a UIImageView containing the image I'd like to display on an iPad screen and am able to consume the user taps just fine. However, my image is that of a hand on a table and I'd like to know if the user has tapped on the hand or on the table part of the image. I can get the x,y coordinates of the user tap with CGPoint tapLocation = [recognizer locationInView:self.view]; but I'm at a loss for how to map that CGPoint to, say, the region of the image that contains the hand vs. the region that contains the table. Everything I've read so far deals with determining if a CGPoint is in a particular rectangular area, but what if you need to determine if that CGPoint is located in the boundaries of a more irregular shape? Is that even possible? Any suggestions or just pointing me in the right direction would be a big help. Thanks!

5条回答
▲ chillily
2楼-- · 2019-04-15 12:47

An extension of the bounding rectangle answer,

  • you could define several smaller bounding rectangles that would approximate a hand without covering the rest of the screen.

OR

  • you could use a list of rectangles, for each of your objects and put the hand at the end of the list. In this case, if you had a tap on button X on the top right hand of the screen which is technically inside the hand rectangle, it would choose the button X because that rectangle is found first.
查看更多
Anthone
3楼-- · 2019-04-15 12:49
  • define the shape by a black and white bitmap (1 bit per pixel). Check if the particular bit is set. This would eat a lot of memory if you had a lot of large shapes, but for one bitmap with a hand, it should not be a big deal.
  • define the shape as a polygon. Then you need to do point-in-polygon test. Wikipedia has a wonderful article on this, with links to code here: http://en.wikipedia.org/wiki/Point_in_polygon
  • iPad libraries might have this already implemented. Sorry, I cannot help you there, not an iPad developer.
查看更多
迷人小祖宗
4楼-- · 2019-04-15 12:52

late to the party, but the core tool you want here is a "point in polygon" routine. this is a generic approach, independent of iOS.

google has lots of info, but the general approach is:

1) define your closed polygon. - it sounds like this might be a bit of work in your case.

2) choose any point not equal to your original point. (yes, any point)

3) for each edge in the polygon, determine if the ray from your original point through the seconds point intersects with that polygon edge. - this requires a line-segment-intersect-ray routine, also available on the 'tubes.

4) if the number of intersections is odd, it's inside the polygon. if the count is even, it's outside.

for general geometry-type issues, i highly recommend Paul Bourke: http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/

查看更多
萌系小妹纸
5楼-- · 2019-04-15 12:56

You could use pointInside:withEvent: to define the hit area programmatically.

To elaborate, you just take the point and evaluate to see if it falls in the area you're after with a series of if statements. If it does, return TRUE. If it doesn't, return FALSE. If this is related to this post, then you could use a circular conditional to compare the distance of the point to the center of your circle using Pythagorean Theorem.

查看更多
Anthone
6楼-- · 2019-04-15 13:05

You can use a bounding rectangle that covers most or all of the hand.

If the user is using his finger to tap either the hand or the table, I doubt that you want him or her to be extremely precise with the tap.

查看更多
登录 后发表回答