Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept touches events on a MKMapView or UIWebView objects?

I'm not sure what I am doing wrong but I try to catch touches on a MKMapView object. I subclassed it by creating the following class :

#import <UIKit/UIKit.h> #import <MapKit/MapKit.h>  @interface MapViewWithTouches : MKMapView {  }  - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event;     @end 

And the implementation :

#import "MapViewWithTouches.h" @implementation MapViewWithTouches  - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {      NSLog(@"hello");     //[super touchesBegan:touches   withEvent:event];  } @end 

But it looks like when I use this class, I see nothing on the Console :

MapViewWithTouches *mapView = [[MapViewWithTouches alloc] initWithFrame:self.view.frame]; [self.view insertSubview:mapView atIndex:0]; 

Any idea what I'm doing wrong?

like image 971
Martin Avatar asked Jun 26 '09 15:06

Martin


1 Answers

The best way I have found to achieve this is with a Gesture Recognizer. Other ways turn out to involve a lot of hackish programming that imperfectly duplicates Apple's code, especially in the case of multitouch.

Here's what I do: Implement a gesture recognizer that cannot be prevented and that cannot prevent other gesture recognizers. Add it to the map view, and then use the gestureRecognizer's touchesBegan, touchesMoved, etc. to your fancy.

How to detect any tap inside an MKMapView (sans tricks)

WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init]; tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {         self.lockedOnUserLocation = NO; }; [mapView addGestureRecognizer:tapInterceptor]; 

WildcardGestureRecognizer.h

// //  WildcardGestureRecognizer.h //  Copyright 2010 Floatopian LLC. All rights reserved. //  #import <Foundation/Foundation.h>  typedef void (^TouchesEventBlock)(NSSet * touches, UIEvent * event);  @interface WildcardGestureRecognizer : UIGestureRecognizer {     TouchesEventBlock touchesBeganCallback; } @property(copy) TouchesEventBlock touchesBeganCallback;   @end 

WildcardGestureRecognizer.m

// //  WildcardGestureRecognizer.m //  Created by Raymond Daly on 10/31/10. //  Copyright 2010 Floatopian LLC. All rights reserved. //  #import "WildcardGestureRecognizer.h"   @implementation WildcardGestureRecognizer @synthesize touchesBeganCallback;  -(id) init{     if (self = [super init])     {         self.cancelsTouchesInView = NO;     }     return self; }  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     if (touchesBeganCallback)         touchesBeganCallback(touches, event); }  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }  - (void)reset { }  - (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event { }  - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer {     return NO; }  - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer {     return NO; }  @end 

SWIFT 3

let tapInterceptor = WildCardGestureRecognizer(target: nil, action: nil) tapInterceptor.touchesBeganCallback = {     _, _ in     self.lockedOnUserLocation = false } mapView.addGestureRecognizer(tapInterceptor) 

WildCardGestureRecognizer.swift

import UIKit.UIGestureRecognizerSubclass  class WildCardGestureRecognizer: UIGestureRecognizer {      var touchesBeganCallback: ((Set<UITouch>, UIEvent) -> Void)?      override init(target: Any?, action: Selector?) {         super.init(target: target, action: action)         self.cancelsTouchesInView = false     }      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {         super.touchesBegan(touches, with: event)         touchesBeganCallback?(touches, event)     }      override func canPrevent(_ preventedGestureRecognizer: UIGestureRecognizer) -> Bool {         return false     }      override func canBePrevented(by preventingGestureRecognizer: UIGestureRecognizer) -> Bool {         return false     } } 
like image 154
gonzojive Avatar answered Sep 22 '22 09:09

gonzojive