Image mapping?

Discussion in 'iOS Development' started by hitman2992, Jun 7, 2009.

  1. hitman2992

    hitman2992 New Member

    Joined:
    Dec 10, 2007
    Messages:
    123
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    I have a question that I was hoping someone could help me out with.

    I need to make only a certain part of an image touchable. how would i do this?
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You could subclass UIImageView and override the touchesBegan function, and if the coordinates of the touch meet some conditions, you make it do something, otherwise, you discard it.
  3. hitman2992

    hitman2992 New Member

    Joined:
    Dec 10, 2007
    Messages:
    123
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    thanks ill try that out
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    You actually wouldn't even have to subclass it for just tracking the point touched. UITouch has a CGPoint value for its location in a specific view, which you can then see if that point lies within a certain CGRect, and then decide what to do from there.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Checking if the touch is within a certain CGRect fails in all cases where you want to map other shapes besides rects (what image mapping is intended for).

    I would, if I were you, create a UIImageView subclass called UIMappedImageView (or whatever) that has a mapCoords:VA_ARGS method. That method would, when called create an array if shapes. I would also categorize NSArray and add a performSelector method on it that takes that selector and performs it on all the array's objects if possible (skipping those objects that don't reapondToSelector:. Back in my UIMappedImageView, I'd override all touch methods, having them forward the touch if the view is in mapped space and ignoring the touches in nonmapped space.

    Furthermore, I would expand the UIMappedImageView class to have a mapCoords:selector: method so you could have different actions per mapped shape.

    Also, keep the array of shapes readonly.
  6. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Reading this thread gave me an idea:
    http://www.ifans.com/forums/showthread.php?t=196086

    Make a subclass of UIImageView and add a new UIImage called touchMap or something like that, that touchMap will be red (255, 0, 0) in areas where you want the touch to be detected. Also, it should be transparent, so the user can't see it.

    Override the touchesBegan method, and make it check what's the color of the pixel touched in the touchMap image, if it's red, do something, if it isn't, do nothing, or something else.

Share This Page