A better way to handle touch of a UIImageView better

Discussion in 'iOS Development' started by Gottaa, Mar 2, 2009.

  1. Gottaa

    Gottaa New Member

    Joined:
    Jan 4, 2008
    Messages:
    21
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I'll start by saying I have pretty much what I want working now, but it just feels like "ugly" code if that makes any sense. I have four images bouncing around the screen, they are stored in an array (Thanks to Skylar while I was reading other threads) which contains the image, and there CGPoint, I do want to add in there direction to the array and there size, but that's something I'm happy to churn through myself till I get my poor maths sorted

    Please Register or Log in to view images

    . Anyway I have the images bouncing and I have them detecting a touch event and reacting as I want, but having to check each item in the array to see if it's been touched seems abit backwards. There does appear to be a sprite control which from a cursory glance would be neater, I think it just sits abit awkardly with me that I'm having to use globals to achieve all this

    Hmm, that was alot of waffle, the basic question is:

    Will a sprite handle the fact it's been touched better without having to loop through an array or am I better off just sticking to an array ?

    Sorry if this is a silly question, my background is in VB.NET and a little C# coding in business so anything to do with graphics and moving of things is pretty foreign to me.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Perform a hit test. They are designed to let you know the farthest descendant of an event, so put it in your touch method.

    - (UIView *)hitTest

    Please Register or Log in to view images

    CGPoint)point withEvent

    Please Register or Log in to view images

    UIEvent *)event

    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [INDENT]UITouch *touch = [touches anyObject];
    UIView *touchedView = [[UIView alloc] init];
    
    touchedView =  [self.view hitTest:[touch locationInView:self.view] withEvent:event];
    
    /*   Do something with your view   */
    
    [touchedView release];[/INDENT]
    }

    EDIT: Or, I may have that backward, but you should know where to go from here if it is. I've never had to use a hit test, so I haven't.
  3. Gottaa

    Gottaa New Member

    Joined:
    Jan 4, 2008
    Messages:
    21
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thanks Skylar,

    I got your idea working, and I'm much happier with it now. That said I'm curious if I'm barking up the wrong tree slightly if it something you've never used yourself, so having kept a copy of the original I'm going to look back at that and see if I can improve that, and if not I have the working hitTest version to carry on with.

Share This Page