I have this image:

What I want to do is to add a UITapGestureRecognizer to this image (or I can split the image in the different parts it consists of and add for each part a UITapGestureRecognizer) in order to have different actions according to the leaf tapped. If I split the image in different images each for each leaf the UIImageViews will probably overlap and tapping on one will be recognized as a tap on another one. Having just one image implies knowing the points of the screen that belongs to a leaf rather than to another one.
Any clues on how to do it would be really appreciated.
Thanks
Change your behavior by examining the gesture recognizer’s
locationInView:.If you handle the image as one unit, implement this in your gesture recognizer call back to decide which “leaf” (if any) was tapped.
If you handle the image as multiple images, you could also implement it in your callback, or you could also implement in, e.g., your delegate’s
gestureRecognizerShouldBegin:to suppress events for touches outside the leaf as drawn.EDIT: I didn’t realize that you might also be looking for assistance on figuring out whether a point lies within a leaf. @PhillipMills is correct on this point: we need to know how you are drawing the image.
FOLLOW-UP: This is somewhat outside my area of expertise.
The easiest approach (from a hit-testing standpoint) is to do what @PhillipMills suggested, using Quartz drawing and
CGPathContainsPoint(). If you have detailed graphics that you need rendered as a PNG, you could certainly construct a simple path that would be (virtually) overlayed to allow hit testing.Your other options, AFAIK, are to do hit testing mathematically, but you would basically be reimplementing
CGPathContainsPoint()but without a path, or to employ various tricks that look at the color of the pixels at your touch point to do hit testing. Googling will turn up some useful results if you go this route, but honestly for a shape as simple as what you’ve drawn, just use someUIBezierPathcode to recreate in code.