Custom Buttons

Discussion in 'iOS Programming' started by jnoxx, Oct 6, 2011.

  1. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #1
    Hey Every1,

    I'm developing an application for little kids, with loads of cutesie images and blah. But, lets say, I have a star or a plane, I want only the plane to be clickable. But.. The question is, how do you actually surround an "image" or "wrap" an image with a button, that will only accept the click within the image bounds.
    First of all. is this possible (i guess it is.)
    Second of all, can't really find something on google (or i'm looking at the wrong places), Just creating an overlay at the back, but what if there is another image overlapping, which should be clickable instead? these are all questions running through my head. So I would really like an answer :)

    greetings, Noxx.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If we look at this in a more general context. When a touch even happens the the process outlined in Event Delivery happens. Essentially the top most view will ask each view whether the event is inside it with pointInside:withEvent:. If you have a custom UIView you can override this. It's then simply a matter of returning true if the point is within the image bounds.

    So that's the issue regarding non-rectangular buttons dealt with. As for overlapping images it's less clear. It would be nice to think that UIViews are sent the pointInside:withEvent: message in order of above/below. I expect they are but I can't find the documentation that states that. I would build a small experimental app to test this hypothesis if I was you.
     
  3. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Got me a bit overwhelmed there. This is a totally new thing for me ;p never thought I had to figure it out, but I guess I will have to ;p the problem is. you're telling me to figure out wether the touch is within the bounds of the image, but that is just the thing I was wondering, if you code an UIImageView, it allways uses a rectangle as frame, so won't it accept the touch events there too?
    because if it would automaticly take only the image with his bounds, you could use CGIntersect with (and then your finger touch event). Or am i wrong.
    Is really no one that have tried this? ;o
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I'm not suggesting you use a UIImageView. Although if you want to you could if you subclass it. UIImageView will return true as you suggest from that method anywhere within the rectangle. So if you subclass you can override that behaviour. I don't think CGIntersect will help: it works on rectangles I think.

    The normal way to do this is to have a bit array that represents the image. Anywhere the image exists you set to 1, everywhere else 0. Then it's simple and fast to check if a point is on the image or not.
     

Share This Page