Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <h2>How to detect any tap inside an MKMapView (sans tricks)</h2> <pre><code>WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init]; tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) { self.lockedOnUserLocation = NO; }; [mapView addGestureRecognizer:tapInterceptor]; </code></pre> <h2>WildcardGestureRecognizer.h</h2> <pre><code>// // WildcardGestureRecognizer.h // Copyright 2010 Floatopian LLC. All rights reserved. // #import &lt;Foundation/Foundation.h&gt; typedef void (^TouchesEventBlock)(NSSet * touches, UIEvent * event); @interface WildcardGestureRecognizer : UIGestureRecognizer { TouchesEventBlock touchesBeganCallback; } @property(copy) TouchesEventBlock touchesBeganCallback; @end </code></pre> <h2>WildcardGestureRecognizer.m</h2> <pre><code>// // 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 </code></pre> <h2>SWIFT 3</h2> <pre><code>let tapInterceptor = WildCardGestureRecognizer(target: nil, action: nil) tapInterceptor.touchesBeganCallback = { _, _ in self.lockedOnUserLocation = false } mapView.addGestureRecognizer(tapInterceptor) </code></pre> <p><strong>WildCardGestureRecognizer.swift</strong></p> <pre><code>import UIKit.UIGestureRecognizerSubclass class WildCardGestureRecognizer: UIGestureRecognizer { var touchesBeganCallback: ((Set&lt;UITouch&gt;, UIEvent) -&gt; Void)? override init(target: Any?, action: Selector?) { super.init(target: target, action: action) self.cancelsTouchesInView = false } override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent) { super.touchesBegan(touches, with: event) touchesBeganCallback?(touches, event) } override func canPrevent(_ preventedGestureRecognizer: UIGestureRecognizer) -&gt; Bool { return false } override func canBePrevented(by preventingGestureRecognizer: UIGestureRecognizer) -&gt; Bool { return false } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload