1
votes

I have tracked down the problem to be the locationInView: on the UITapGestureRecognizer always giving me CGPoint of (0,-67) in portrait and (0,268) in landscape. (If i don't cast it as UITapGestureRecognizer, i get (0,180) in landscape occasionally.

This problem does not exist in iOS 5 Simulator. It happens often in iOS 6 Simulator and almost 90% of the time in an iOS 6 Device.

My guess now is that the gesture recognizer is not valid anymore by the time it calls the action method.. but that doesn't make sense as that means we always need to call the locationInView: in the delegate methods..

Details:

What I'm trying to do: Recognize a Tap gesture on a MKMapView, covert that point to coordinate and display it as an annotation

What I did: In the action method of the gesture recognizer..

CLLocationCoordinate2D coordinate = [self.mapView convertPoint:[(UITapGestureRecognizer *)sender locationInView:self.mapView] toCoordinateFromView:self.mapView];

I did do introspection to make sure that the sender is indeed an UITapGestureRecognizer..

I also did try return YES for (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer but that does not help.

What should happen: The coordinate should be corresponding to the point tapped on the map.

What did happen: The coordinate is always slightly far off to the left.

Update:

So.. i have tested with all the delegate methods in <UIGestureRecognizerDelegate>. Both in the action method as above and – gestureRecognizer:shouldReceiveTouch: the gesture recognizer gives invalid position (0,-64) for locationInView:. (it was 0,-67 as stated above, but become 0,-64 after i updated Xcode to the latest version few minutes ago, lol) However, in – gestureRecognizerShouldBegin: and – gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: it gives the correct CGPoint.

My question is.. is this the intended behavior? Or did i mess up something? Else it means i need to fire my action in one of the delegate methods, as i do need the correct position of the gesture recognizer. This option doesn't sound very right, though..

1

1 Answers

2
votes

Sorry for the troubles guys, I have found out the cause.

I simply have left locationInView: in a performBlock: of a NSManagedObjectContext, which is clear, as UIGestureRecognizer is a UI thing, and the Rule #1 in iOS is "UI stuffs only belong to the main thread." This immediately explains the inconsistent behavior of the locationInView: and why it is more likely to succeed in the earlier stage..

Lesson learned, again: Read "gesture recognizer" as UIGestureRecognizer. "UI".