Touches on an image

Discussion in 'iOS Programming' started by macfanboy, Jul 30, 2008.

  1. macrumors 6502a

    Joined:
    Jun 5, 2007
    #1
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

    Can you specify an action if a touch is on an image? I have randomly appearing images and I can't figure it out. Btw if you didn't realize it by now, I'm not a master at objective-c. Thanks for taking the time to help a noob!
     
  2. macrumors 68000

    Joined:
    Nov 3, 2007
    #2
    You could always have a button display at a random location, with the image as the button background.
     
  3. thread starter macrumors 6502a

    Joined:
    Jun 5, 2007
    #3
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

    wouldn't it be easier just to use an image and use this?
    Code:
    if (UITouch *is on the image*)
    I know that's not the code but it seems easier. I guess I'm just asking the code to see if a touch is on the image
     
  4. thread starter macrumors 6502a

    Joined:
    Jun 5, 2007
    #4
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

    anything?
     
  5. macrumors regular

    Joined:
    Jun 18, 2008
    #5
    Yes, just enable user interaction for your UIImageView, then implement the touchesBegan method on it's containing view and it will get called and you can check to see if the touch was within the bounds of your UIImageView. If so, then you do something.
     
  6. thread starter macrumors 6502a

    Joined:
    Jun 5, 2007
    #6
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

    Wow I'm so stupid. Thank you for helping me realize I couldve used the method on the imageview itself. That makes me feel stupid
     
  7. macrumors member

    Joined:
    Jul 9, 2008
    #7
    UIImageView derived class

    I am using a UIImageView-derived class and want to handle the touch event in the class, itself for better encapsulation.

    I added the touchesBegan method on my class, but it never gets called. I am setting the userInteractionEnabled to YES on the object.

    The touch event seems to pass to the UITableView that contains my control. So I end up with a cell selection, instead of an event in my control.

    Anything else I should do to get touch events on my control?
     
  8. thread starter macrumors 6502a

    Joined:
    Jun 5, 2007
    #8
    i found somethin new:
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
         UITouch *touch  = [[event allTouches] anyObject];
         if (CGRectContainsPoint([imageView frame], [touch locationInView:self])) {
              [self YOURMETHODHERE];
    }
    
    this looks like it works, although i havent tried it
     
  9. macrumors newbie

    Joined:
    Jun 20, 2007
    #9
    solution???

     
  10. macrumors member

    Joined:
    Jul 9, 2008
    #10
    Use a button

    I just put a button on top of my UIImageView. It is the save size and the image, and has no text or image. Now, when there is a touch event on the image, the button handles it.

    I don't know if that is the best way to do it, but it works.
     
  11. macrumors newbie

    Joined:
    Jun 20, 2007
    #11
    That strikes me as an acceptable solution, but my problem is a bit different. I want to create a slider-like custom control. It would operate vertically and have its own graphics. I've written all the pieces, but I can't activate it.

    The answer is probably in understanding how the "first responder" works, but the Apple documentation is vague about that.
     
  12. macrumors member

    Joined:
    Jul 9, 2008
    #12
    Custom control

    Mine was a custom control, as well.

    Unfortunately, I abandoned it as my app evolved, so I no longer have the source for the button.

    I can't recall if I derived from UIButton and added the UIImageView from within the button or if I derived from UIImageView and added the button from within the UIImageView.

    I think I derived from UIButton and added the UIImageView from within the button. So from the control-consumer's perspective, it's just a button. But when it is clicked on, I trigger the animation from the UIImageView before passing the click event up to the containing view.

    I probably had the same goal as you: a single, reusable control that acted like a button, but allowed by to animate between states on a touch event. :)
     
  13. macrumors newbie

    Joined:
    Jun 20, 2007
    #13
    I think I derived from UIButton and added the UIImageView from within the button. So from the control-consumer's perspective, it's just a button. But when it is clicked on, I trigger the animation from the UIImageView before passing the click event up to the containing view.

    I probably had the same goal as you: a single, reusable control that acted like a button, but allowed by to animate between states on a touch event. :)[/QUOTE]

    Tried that -- inherit from UIButton, give it the size and image I want, write my own "touchesBegan" etc. and it worked! I even reset the image at points to give it a pseudo animation. Just what I wanted.
     

Share This Page