How to add gesture recognizer to a UIImageview?

22,723

Solution 1

Your UIView obviously intercept the touches before they can reach the UIImageView.

You can either put your UIImageView in front of your UIView, or disable user interaction on the UIView so it does not intercept the touches.

If you need finer grain over catching your touch events, you may either:

  • put your UIView on top of the UIImageView if it has to for design purposes (if it masks the UIImageView a bit for example) but make it not catch events (userInteractionEnabled = NO), and use a different UIView below the UIImageView to actually catch the events outside the UIImageView
  • keep your UIView on top of the UIImageView and keep it catching events (userInteractionEnabled = YES), but filter the events that pass thru it, by subclassing your UIView and overriding either the pointInside:withEvent: or the hitTest:withEvent: method.

For more information you should really read the dedicated Apple Programming Guide related to Event Handling.

Solution 2

my code:

UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(hasLongPressed)];
[press setMinimumPressDuration:0.1f]; //1ms
press.delegate = (id)self;
[self.DGEMLogo addGestureRecognizer:press];

two properties are neccessary to do

1.) add the GestureRecognizer to your UIImageView (self.DGEMLogo) [self.DGEMLogo addGestureRecognizer:press];

2.) set your UIImage property Interaction enabled to TRUE

thats all.

Share:
22,723
pqteru
Author by

pqteru

A writer and a developer in the field of software and Internet.

Updated on October 13, 2020

Comments

  • pqteru
    pqteru over 3 years

    In the project, there are an UIView myView and an UIImageView myImage behinds the myView, views hierarchy:

    UIWindow
    |-- UIImageView (myImage)
    |-- UIView (myView) [whole screen]

    Then added a gesture recognizer to myImage in ViewController

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            tapGesture.numberOfTapsRequired = 1;
            [myImage addGestureRecognizer:tapGesture];
    

    But gesture doesn't affect myImage, myImage has setUserInteractionEnabled:YES. It will only affect when myImage placed in front of myView, how can I solve this problem?

  • pqteru
    pqteru over 11 years
    but myImage has to stay behind myView, is there a way that I can make a detection while touch start and to determine which view is going to affect?
  • AliSoftware
    AliSoftware over 11 years
    Yes. As I stated in my answer, turn user interaction off (userInteractionEnabled = NO) on the UIView. If the UIView still needs to capture touch events anywhere except where the UIImageView lies, you have multiple possibilities: (1) make your UIView on top of the UIImageView but with userInteractionEnabled = NO and put another UIView below the UIImageView that capture every touch events that haven't been captured by the UIImageView or (2) put the UIView on top of the UIImageView, subclass it & override the hitTest:withEvent: or pointInside:withEvent: UIView methods