Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the coordinates of UIImage from a UITapGestureRecognizer in a zoomed image?

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];
}
like image 934
P. Sami Avatar asked Dec 17 '22 16:12

P. Sami


1 Answers

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.

like image 177
P. Sami Avatar answered Feb 01 '23 23:02

P. Sami