Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>For a MKMapView the real working solution is with gesture recognization !</h2> <p>Me I wanted to stop updating the center of the map on my location when I drag the map or pinch to zoom.</p> <p>So, create and add your gesture recognizer to the mapView :</p> <pre><code>- (void)viewDidLoad { ... // Add gesture recognizer for map hoding UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAndPinchGesture:)] autorelease]; longPressGesture.delegate = self; longPressGesture.minimumPressDuration = 0; // In order to detect the map touching directly (Default was 0.5) [self.mapView addGestureRecognizer:longPressGesture]; // Add gesture recognizer for map pinching UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAndPinchGesture:)] autorelease]; pinchGesture.delegate = self; [self.mapView addGestureRecognizer:pinchGesture]; // Add gesture recognizer for map dragging UIPanGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)] autorelease]; panGesture.delegate = self; panGesture.maximumNumberOfTouches = 1; // In order to discard dragging when pinching [self.mapView addGestureRecognizer:panGesture]; } </code></pre> <p>Look the <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html" rel="noreferrer">UIGestureRecognizer Class Reference</a> to see all available gesture recognizer.</p> <p>Because we've defined the delegate to self, we have to implement the protocole UIGestureRecognizerDelegate :</p> <pre><code>typedef enum { MapModeStateFree, // Map is free MapModeStateGeolocalised, // Map centred on our location MapModeStateGeolocalisedWithHeading // Map centred on our location and oriented with the compass } MapModeState; @interface MapViewController : UIViewController &lt;CLLocationManagerDelegate, UIGestureRecognizerDelegate&gt; { MapModeState mapMode; } @property (nonatomic, retain) IBOutlet MKMapView *mapView; ... </code></pre> <p>And override the methode gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: in order to allow recognize multiple gestures simultaneously, if I understood right :</p> <pre><code>// Allow to recognize multiple gestures simultaneously (Implementation of the protocole UIGestureRecognizerDelegate) - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } </code></pre> <p>Now write the methodes which will be called by our gesture recognizers :</p> <pre><code>// On map holding or pinching pause localise and heading - (void)handleLongPressAndPinchGesture:(UIGestureRecognizer *)sender { // Stop to localise and/or heading if (sender.state == UIGestureRecognizerStateBegan &amp;&amp; mapMode != MapModeStateFree) { [locationManager stopUpdatingLocation]; if (mapMode == MapModeStateGeolocalisedWithHeading) [locationManager stopUpdatingHeading]; } // Restart to localise and/or heading if (sender.state == UIGestureRecognizerStateEnded &amp;&amp; mapMode != MapModeStateFree) { [locationManager startUpdatingLocation]; if (mapMode == MapModeStateGeolocalisedWithHeading) [locationManager startUpdatingHeading]; } } // On dragging gesture put map in free mode - (void)handlePanGesture:(UIGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan &amp;&amp; mapMode != MapModeStateFree) [self setMapInFreeModePushedBy:sender]; } </code></pre>
    singulars
    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. 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