Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add gesture recognizer to a UIImageview?

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?

like image 405
pqteru Avatar asked Sep 09 '12 12:09

pqteru


People also ask

How do I add tap gestures in Objective C?

Adding a Tap Gesture Recognizer to an Image View in Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the image view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

How do I add swipe gestures in Swift?

Fire up Xcode and create a blank project by choosing the App template from the iOS > Application section. Name the project Swipes, set Interface to Storyboard, and Language to Swift. Open ViewController. swift and declare a private, constant property with name swipeableView of type UIView .


Video Answer


1 Answers

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.

like image 131
AliSoftware Avatar answered Sep 28 '22 21:09

AliSoftware