views:

117

answers:

1

I have a PNG in a UIImageView with alpha around the edges (let's say a circle). When I tap it, I want it to register as a tap for the circle if I'm touching the opaque bit, but a tap for the view behind if I touch the transparent bit.

(BTW: On another forum, someone said PNGs automatically do this, and a transparent PNG should pass the click on to the view below, but I've tested it and it doesn't, at least not in my case.)

Is there a flag I just haven't flipped, or do I need to create some kind of formula: "if tapped { get location; calculate distance from centre; if < r { touched circle } else { pass it on } }"?

-k.

+2  A: 

I don't believe that PNGs automatically do this, but can't find any references that definitively say one way or the other.

Your radius calculation is probably simpler, but you could also manually check the alpha value of the touched pixel in your image to determine whether to count it as a hit. This code is targetted at OS X 10.5+, but with some minor modifications it should run on iPhone: Getting the pixel data from a CGImage object. Here is some related discussion on retrieving data from a UIImage: Getting data from an UIImage.

pix0r
I have found that on the Simulator, the transparent pixels are ignored on mouse clicks. This doesn't seem to happen on the device, but I'm not sure.
pgb
Comment on comment: I did a test with a 50px circle in a 300px png, the circle took all the clicks on the device. But then again, just getting started, I've been having issues with my touch events, so it could be me.
Kevin Beimers
Comment on answer: thanks for being so comprehensive, I've only ever asked 2 questions to this forum and been very happy with the responses both times. The apple developer forums seem to be full of answers like "go read a book" or "if you knew anything about modal run loops you'd have solved it by now." So, big thanks. -k.
Kevin Beimers