devios1
devios1

Reputation: 38025

UIPanGestureRecognizer translationInView always returning a relative point

I have a UIPanGestureRecognizer attached to a map view in my window (and recognizing gesture simulateously so I can receive events when the map pans). However for some reason I can't figure out, [UIPanGestureRecognizer translationInView:] always returns coordinates relative to the origin of the pan. In fact it behaves identically regardless of what view parameter I pass to it, even nil!

I can't figure out why it's not telling me coordinates relative to the view I pass in.

self.mapViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
self.mapViewPan.delegate = self;
[self.mapView addGestureRecognizer:self.mapViewPan];

*

- (void)pan:(UIPanGestureRecognizer*)sender {
    CGPoint translatedPoint = [sender translationInView:self.view];
    NSLog(@"%f, %f", translatedPoint.x, translatedPoint.y);
}

Upvotes: 0

Views: 1119

Answers (2)

devios1
devios1

Reputation: 38025

Nevermind, I seem to have misunderstood what translationInView: actually represents. It appears to be relative to the origin of the touch regardless (which makes me wonder what its parameter actually affects).

It turns out what I really want is locationInView::

CGPoint touchPoint = [sender locationInView:self.view];

This returns the location of the touch in the view's coordinates.

Upvotes: 3

atreat
atreat

Reputation: 4413

Is there any reason you're not using the MKMapViewDelegate methods for this? mapView:regionDidChangeAnimated: should let you get the map region when it scrolls.

Upvotes: 0

Related Questions