Reputation: 279
I'm working on a little app, on which I have a UIScrollView - and inside this, an image, twice as wide, displaying a map. The UIScrollView is made in the Storyboard Editor, the image is dynamically loaded.
It works partially - I can pan, zoom and detect a click, but I need it to return the x/y coordinates, reflecting the touch position.
Here are the relevant part of my code:
UIImage *worldMapImage = [UIImage imageNamed:@"worldmapBig.png"];
imageView = [[UIImageView alloc] initWithImage:worldmapImage];
imageView.userInteractionEnabled = YES;
imageView.multipleTouchEnabled = YES;
[_worldmap addSubview:imageView];
_worldmap.userInteractionEnabled = YES;
[_worldmap setContentSize:[worldMapImage size]];
[_worldmap setMinimumZoomScale:.5];
[_worldmap setMaximumZoomScale:1.0];
[self performSelectorOnMainThread:@selector(didLoadImageInBackground:) withObject:imageView waitUntilDone:YES]; // After loading, initially zoom out
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture];
}
-(void)tapDetected:(UIGestureRecognizer*)recognizer{
UIView *myView = [recognizer view]; <--
CGPoint touch = [recognizer locationInView:myView]; <--
NSLog(@"%f", touch.x);
}
I've searched a LOT of webpages, addressing a similar problem - and also tried replacing the image with a button, using the map as background. It returns the coordinates, but then I can't pan/zoom anymore; haven't been able to get both coordinates and pan/zoom functionality in the same code.
Can you help me out here? I've been stuck for quite some days...
UPDATE: the event handler has been changed - now it works :-)
Upvotes: 0
Views: 943
Reputation: 52221
Perhaps try this.
The -[UIGestureRecognizer locationInView:] method computes the center point of all the touche points in the gesture.
Upvotes: 1