0

I have a UIButton whose type is custom, and is represented by a png file, part of which is transparent.

Naturally, I want my button to activate the touchUpInside event if and only if the non transparent (=visible) part is tapped. But I get the event also when I touch the transparent part, which is unacceptable.

How can I make my button aware only of its visible part?

jkally
  • 794
  • 2
  • 9
  • 35

1 Answers1

0

Strip your image into two parts. One that is the button (clickable-part) and one that is the background. Set the button images for only the clickable area. Place the UIButton on top of an UIImageView with the background on it. You can inherit from UIButton to make your own control that is a UIButtonWithBackground like above and then override the state functions to change the background state when the button changes state too.

madmik3
  • 6,975
  • 3
  • 38
  • 60
  • Sorry, but the clickable area is not rectangular. Let's say its a ccircle. I only want clicks on the circle to fire the event, not in the frame but outside the circle. – jkally Jan 31 '11 at 15:49
  • that's a bit of a harder case. I don't think UIButton will do this for you. You can however load the displayed image into a UIImage and when the users finishes there touch you can access the alpha value of the UIImage of the corresponding pixel to determine if it was visible. You can inherent from UIImageView and watch the touch events to solve this issue. – madmik3 Jan 31 '11 at 16:35
  • "access the alpha value of the UIImage of the corresponding pixel to determine if it was visible" and HOW do you do just that? can you refer me to any samples? – jkally Jan 31 '11 at 17:05
  • 1
    this will show you how. http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics – madmik3 Jan 31 '11 at 21:42