MkMapView点击+注释选择问题

时间:2011-09-23 12:35:08

标签: iphone ios annotations mkmapview mapkit

我有一个MkMapView,我有一些注释。单击注释时,其详细信息在另一个视图中打开,该视图在地图视图的下半部分具有滚动视图。当我们滚动滚动视图时,地图以下一个注释为中心,其详细信息显示在scrollview中。

我的问题是我想在地图上添加一个点击手势,这样当我点击地图时,滚动视图应该隐藏。为此,我在地图上添加了一个UiTapGesture也可以正常工作,但问题是地图上的注释不再保持可用。地图总是进行tapgesture的动作,它再也不会再调用selectannotation方法了吗?

如何解决此问题????

3 个答案:

答案 0 :(得分:4)

通过实施shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法,您可以告诉您的手势识别器和地图同时工作。

创建点击手势时,请设置其委托:

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

并实施方法:

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

现在,您的点按手势方法和didSelectAnnotationView都会被调用。

假设您的点击处理程序首先被调用,您可以删除并取消滚动视图,然后didSelectAnnotationView将创建并添加scrollview。如果序列结果不同,您可能需要添加一些标志来协调删除/创建。

答案 1 :(得分:3)

不是一种干净的方式,但我能找到的唯一方法是检查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;
}

答案 2 :(得分:0)

我认为你应该只在滚动视图显示时添加手势识别器。就像我在下面的例子中使用键盘一样 1.当键盘显示时,mapView会添加一个轻击手势 2.离开时我删除了手势识别器。

// 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];
}