Intercepting/Hijacking iPhone Touch Events for MKMapView

后端 未结 6 1418
故里飘歌
故里飘歌 2020-12-01 05:56

Is there a bug in the 3.0 SDK that disables real-time zooming and intercepting the zoom-in gesture for the MKMapView? I have some real simple code so I can detect tap event

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 06:24

    The best way I have found to achieve this is with a Gesture Recognizer. It's unclear if you want to recognize zoom events yourself or just detect when the user is zooming/panning.

    I don't need to detect a map pan or zoom--i just care if the user has put a finger down anywhere in the MKMapView. If you need to detect zooming or panning with 100% recall and precision, it might be more complicated than this. What we really need is an open source implementation of MKMapView so we can add this to the delegate, among many other features.

    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 deal with the relevant touch events as you desire.

    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 
    
    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
    

提交回复
热议问题