question about the touch of a set of images

Discussion in 'iOS Programming' started by tranvutuan, Dec 24, 2011.

  1. tranvutuan macrumors member

    Joined:
    Dec 19, 2011
    #1
    Hi everyone
    First, Merry Christmas and Happy Holiday.....
    My question is very simple :I have 4 images(UIIMAGEVIEW) and i would like to know which image has been touched
    Please advice me.
    Thanks
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Please show us the relevant code you've written so we know where you need help.

    (These forums need a bot that automatically asks for us...)
     
  3. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #3
    Ok let me make it all clear. I am having 4 images ( UIImageView).
    Code:
    MyImage.h
    
    #import <UIKit/UIKit.h>
    
    @interface ItemDetailViewController : UIViewController
    @property (weak, nonatomic) IBOutlet UIImageView *myImg1;
    @property (weak, nonatomic) IBOutlet UIImageView *myImg2;
    @property (weak, nonatomic) IBOutlet UIImageView *myImg3;
    @property (weak, nonatomic) IBOutlet UIImageView *myImg4;
    
    @end
    
    
    All I want is if i click on myImage1, it will show on the console that
    Code:
    myImg1 has just been touched
    I used method below
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    However, it is used to detect a touch but it wont tell you which image has been touched.... Any ideas about this.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Hmmm... use UIButtons instead.

    You're using a storyboard or xib file, right? Just use UIButtons, set the type to custom, and assign it the proper image. Visually, it'll look the same, but it'll be a lot easier to figure out which one was being touched.

    Add a method like:
    Code:
    - (IBAction)image:(UIButton *)sender touchedWithEvent:(UIEvent *)event
    and then connect your button within your storyboard or xib to the controller so that it calls at the appropriate time. (Probably for "Touch Down")

    Within the method you can figure out which image sent it by checking what "sender" is.
     
  5. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #5
    I am using StoryBoard. If switching to UIButton, there is no problem for me.However, I just want to know if there are any other mechanisms to detect which UIImageView has been touched though.
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    If you looked into the set touches (from the method you were originally using), you'd be able to find the location on the screen that was touched. You could then check whether the frame of each image contains the point that was touched. It'd be more difficult to program and probably wouldn't work as quickly as just using UIButtons.
     
  7. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    Also, UIImageViews can't receive touches with your call afaik.
    Because UIImageViews their basic property is "UserInteractionEnabled = NO"
    So you need to set it to yes in your IB, or code it like that.
    Second, like warfare said, i think it's better (if it's just 4 images), to use Custom UIButton's. And set an image to them. Then you can hook up the same method for each, but via the IBAction:(id)sender -> cast a new UIButton like this
    Code:
    UIButton *button = (UIButton*)sender;
    then you can switch through your tags of the sender like this.
    Code:
    switch (sender.tag)
    blalala/rest of switch case
    And if you have set up the tags of your buttons like you wanted, you can play with those. And trigger seperate things based upon the button you want.
     

Share This Page