MkMapView tap + Edit annotation

I have an MkMapView on which I have some annotations. When I click on the annotation, its detail opens in another view, which has a scroll view in the lower half of the map display. When we view the scrollview, the map focuses on the next annotation, and its details are displayed in the scrollview.

My problem is that I want to add a snap gesture on the map, so that when I click on the map, the scroll should be hidden. To do this, I added UiTapGesture to the map, which also works fine, but the problem is that the annotations on the map no longer remain hidden. Does a card always go into tapgesture action and never call the selectannotation method?

How can I fix this problem?

+5
source share
3 answers

You can tell your gesture recognizer and card to work simultaneously by executing the delegate method shouldRecognizeSimultaneouslyWithGestureRecognizer.

When creating tap gestures, set its delegate:

tapGR.delegate = self;  //also add <UIGestureRecognizerDelegate> to @interface

and implement the method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer
        :(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Now your gesture method didSelectAnnotationViewwill be called.

Assuming your crane handler is called first, you can delete and skip scrollview, and then didSelectAnnotationView will create and add scrollview. If the sequence is different, you may need to add several flags to coordinate deletion / creation.

+4
source

, , , - shouldBeginGestureRecognizer:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.mapView];
    NSLog(@"touch %f %f",p.x,p.y);

    MKMapRect visibleMapRect = self.mapView.visibleMapRect;
    NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:visibleMapRect];

    for ( MyCustomAnnotation *annotation in visibleAnnotations ){

        UIView *av = [self.mapView viewForAnnotation:annotation];
        if( CGRectContainsPoint(av.frame, p) ){
            // do what you wanna do when Annotation View has been tapped!
            return NO;
        }
    }

    return YES;
}
+3

I think you only need to add a gesture recognizer when scrolling is shown. Like me with the keyboard in the example below 1. When the keyboard is displayed, mapView adds a click gesture 2. When you delete, I delete the gesture recognizer.

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    self.tapMapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    self.tapMapGestureRecognizer.cancelsTouchesInView = NO;
    [self.parkingsMapView addGestureRecognizer:self.tapMapGestureRecognizer];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self.parkingsMapView removeGestureRecognizer:self.tapMapGestureRecognizer];
}

-(void) hideKeyboard{
    [self.searchbar resignFirstResponder];
}
0
source

All Articles