4

I have a UIScrollView and inside it there is a UIImageView with an image. I would like to be able to zoom and still be able to get the coordinates of UIImage. My Image has coordinates bigger than iPhone screen 600x800 and with the current method, i only get the coordinates on the iPhone screen. How can I get the coordinates of the actual place on the UIImage that has been tapped?

This is what I've got so far:

- (void) loadView {

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;


UIImage *mapImage = [UIImage imageNamed:@"Map1.png"];
imageV = [[UIImageView alloc] initWithImage:mapImage];
//imageV.userInteractionEnabled = YES;

//[imageV addGestureRecognizer:tapGesture];

//CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
scrollV = [[UIScrollView alloc] initWithFrame:imageV.frame];
scrollV.contentSize = CGSizeMake(imageV.frame.size.width, imageV.frame.size.height);

[scrollV addGestureRecognizer:tapGesture];

[scrollV addSubview:imageV];
scrollV.userInteractionEnabled = YES;
scrollV.maximumZoomScale = 5;
scrollV.minimumZoomScale = 0.5;
scrollV.bounces = NO;
scrollV.bouncesZoom = NO;
scrollV.delegate = self;

self.view = scrollV;

}



-(void)tapDetected:(UIGestureRecognizer*)recognizer{
NSLog(@"tap detected.");
CGPoint point = [recognizer locationInView:nil];

NSLog(@"x = %f y = %f", point.x, point.y );
}

-(UIView*) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [[self.view subviews] objectAtIndex:0];
}

1 Answer 1

12

I just figured out that the uitapgesture recognizer should be added to the UIImageView and when the method tapDetected is being called, the right way to get the location is to give it the right view as below

CGPoint point = [recognizer locationInView:self.imageV];

Then it is possible to get the coordination from the image.

3
  • Thanks for reposting this. Your correction helped me quickly get my point from a UITapGesture.
    – Nungster
    Oct 10, 2011 at 13:03
  • P.sami How to handle the same problem in android ? Jan 12, 2012 at 14:31
  • sorry i'm replying so late Arunkumar.p... I really have no idea :(
    – P. Sami
    Jan 31, 2012 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.