6

I have a MKMapView in a ViewController and would like to detect users' gestures when he/she touches the map with these methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

The app works fine with iOS 3, iOS 4 but when I debug the app with iPhone running on iOS 5, I see this message:

Pre-iOS 5.0 touch delivery method forwarding relied upon. Forwarding -touchesCancelled:withEvent: to <MKAnnotationContainerView: 0x634790; frame = (0 0; 262144 262144); autoresizesSubviews = NO; layer = <CALayer: 0x634710>>

and the code in the above 4 methods are not reached.

Do you know how to fix it?

Thanks.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • 1
    Can't comment on iOS 5 yet but for 3.2 to 4, it may be easier to use a UIGestureRecognizer instead of the touches methods. –  Sep 22 '11 at 21:27
  • http://stackoverflow.com/questions/1049889/how-to-intercept-touches-events-on-a-mkmapview-or-uiwebview-objects.. Check this Link – Kalpesh Oct 19 '12 at 11:54

1 Answers1

1

Some form of UIGestureRecognizer can help you out. Here's an example of a tap recognizer being used on a map view; let me know if this isn't what you're looking for.

// in viewDidLoad...

// Create map view
MKMapView *mapView = [[MKMapView alloc] initWithFrame:(CGRect){ CGPointZero, 200.f, 200.f }];
[self.view addSubview:mapView];
_mapView = mapView;

// Add tap recognizer, connect it to the view controller
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapViewTapped:)];
[mapView addGestureRecognizer:tapRecognizer];

// ...

// Handle touch event
- (void)mapViewTapped:(UITapGestureRecognizer *)recognizer
{
    CGPoint pointTappedInMapView = [recognizer locationInView:_mapView];
    CLLocationCoordinate2D geoCoordinatesTapped = [_mapView convertPoint:pointTappedInMapView toCoordinateFromView:_mapView];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            /* equivalent to touchesBegan:withEvent: */
            break;

        case UIGestureRecognizerStateChanged:
            /* equivalent to touchesMoved:withEvent: */
            break;

        case UIGestureRecognizerStateEnded:
            /* equivalent to touchesEnded:withEvent: */
            break;

        case UIGestureRecognizerStateCancelled:
            /* equivalent to touchesCancelled:withEvent: */
            break;

        default:
            break;
    }
}
Dany Joumaa
  • 2,030
  • 6
  • 30
  • 45