11

I have a UIImageView in a view controller. Is it possible to make certain areas of the image view tappable?

Example : I have an image of a map. Make only the POIs tappable. Not the whole image. Is this possible?

Isuru
  • 30,617
  • 60
  • 187
  • 303
  • You can add an `UIView` on that area and give its color to `clearColor` and add tapGesture on it ... – TheTiger Jun 26 '13 at 04:16
  • I've already tried this method :) The problem is, say if I zoom in on the image, the view stays the same spot while the image resizes. Another problem I encountered in this way is if I have a larger image in a scrolliew, if I put a `UIView` on top of the `UIScrollView`, the scrolling will stop working. – Isuru Jun 26 '13 at 04:23
  • You can calculate the frame of that view after zoomed in or out ... multiple height and width by zoom scale .. You will have to do some logis here :) – TheTiger Jun 26 '13 at 04:27
  • [tap on part of view](https://stackoverflow.com/a/19085354/6521116) – LF00 May 31 '17 at 10:28

4 Answers4

18

You can use the handleGesture method. First you need to create a location to receive the touches and you have to compare it with touch location in the delegate method as below:

CGRect locationRect;

in viewdidload

locationRect = CGRectMake(CREATE A FRAME HERE);

next the delegate method

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.view];
    if (CGRectContainsPoint(locationRect, p)) {
        NSLog(@"it's inside");
    } else {
        NSLog(@"it's outside");
    }
}
manujmv
  • 6,450
  • 1
  • 22
  • 35
3

Thanks to manujmv, I was able to figure out Swift 3 implementation of a Custom Gesture Area. In my case I'm creating 50 point strips on each side of the window to move from one VC to another. But it should be pretty easy to reuse this for any other application:

class ViewController: UIViewController {
   ...
   var mySensitiveArea: CGRect?
   ...
   override func viewDidLoad() {
   ...
   let screenWidth = UIScreen.main.bounds.size.width
   let screenHeight = UIScreen.main.bounds.size.height
   mySensitiveArea = CGRect(0, 0, 50, screenHeight)
   let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(_:)))
   swipeGesture.direction = UISwipeGestureRecognizerDirection.right
   self.view.addGestureRecognizer(swipeGesture)
   }
}

//Function for determining when swipe gesture is in/outside of touchable area
func handleGesture(_ gestureRecognizer: UIGestureRecognizer) {
    let p = gestureRecognizer.location(in: self.view)
    if mySensitiveArea!.contains(p) {
        print("it's inside")
        showMainViewController()
    }
    else {
        print("it's outside")
    }
}

//Segue to Main VC
func showMainViewController() {
    self.performSegue(withIdentifier: "toMain", sender: self)
}
Repose
  • 2,157
  • 1
  • 24
  • 26
2

Yes it is possible, but you should ask yourself whether or not it is worth it. If it were me, I would add a point of interest object onto the map and attach a gesture recognizer to that instead. However, if you want to go the other route you can look into the following method of UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

This will say whether or not the gesture should process a given touch. You can filter it based on your POIs.

borrrden
  • 33,256
  • 8
  • 74
  • 109
0

yeah i can help you. Recently i tried to create a custom segmentedControl where i had three views in it.

WHILE I WAS TOUCHING FIRST HALF OF THE VIEW THE tapGesture WORKED. But the rest half dint respond. I checked it and the result is useful to you now.

My frame of the segment is:

segment.frame=CGRectMake(0, 0, 300, 100) ;

And the frame of the UIView is:

view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 80, 100, 40)];

Then i changed the height of the frame to 200 And then it worked:

segment.frame=CGRectMake(0, 0, 300, 200) ;

** SO IF YOU WANT TO MAKE, TAP-RECOGNITION ONLY TO PART OF YOUR VIEW, THE REDUCE THE HEIGHT OF YOUR FRAME THATS IT **