-2

I have to incorporate an image of a map(overview of a place, building etc) into a image view. There will be places in the map which should be selectable. Typically an image has 20-25 places which has to be made selectable. The shape of the places will differ to each other.

In html we have the map tag to accomplish this kind of tasks, I need a solution for this in iOS native platform. Any suggestions or help will be great!

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
jegadeesh
  • 875
  • 9
  • 22

2 Answers2

1

Key idea : You can add UITapGestureRecognizer to UIImageView. Setting up a selector which will be fired for each tap. In the selector you can check for the co-ordinate where the tap was done. If the co-ordinate satisfy your condition for firing up an event, you can execute your task then.

Adding the gesture recognizer:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImgViewTap:)];
[singleTap setNumberOfTapsRequired:1];

[yourImgView addGestureRecognizer:singleTap];

Setting up the selector:

-(void)handleImgViewTap:(UITapGestureRecognizer *)gestureRecognizer

{
    // this method gonna fire everytime you tap on the 
    // image view. you have to check does the point where
    // the tap was done, satisfy your path/area condition.

    CGPoint point = [gestureRecognizer locationInView:yourImgView];

    // here point.x and point.y is the location of the tap
    // inside your image view. 
    if(/*your condition goes here*/)
    {
         // execute your staff here.
    }
}

Hope it helps, Happy ios coding.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • My problem is, I have to setup the clickable area first. That area won't be in a rectangle shape. It will be in a polygon shape and when the user taps a particular area (A place which user can do some actions on it.) I have to highlight that place separately(using a border line or highlighting that area). For that I have to first define the areas in the image view which a user can tap. What I'm imagining is to define a polygon inside the image view and once a tap is made, just compare if the hit point is present in any of the polygons in the map. Now any idea in doing that? Thanks for d effort – jegadeesh Oct 24 '17 at 10:19
  • For ploygon you can make a convex hull first. When the tap is made you can decide whether the points lies inside the convex hull or not. If the point lies inside the hull then execute the action designated for that area. – Ratul Sharker Oct 24 '17 at 10:25
  • Yes I know that. But, how to implement that in my scenario? – jegadeesh Oct 24 '17 at 10:27
  • Please read the comment carefully. Code for checking "is a point inside a convex hull" is common problem set, and even stackoverflow has it . https://stackoverflow.com/questions/28801832/determine-whether-a-point-lies-in-a-convex-hull-in-olog-n-time/28801885#28801885 – Ratul Sharker Oct 24 '17 at 10:44
0

Given a view (ora imageview) you should define a UIBezierPath of your shape. Add a taprecognizer to this view, and set the same view as the recognizer delegate. In the delegate method use UIBezierPath.contains(_:) to know if the tap is inside the path or not and decide to fire the tap event or not.

Let me know if you need code example.

iGenio
  • 398
  • 1
  • 8