自定义标注手势识别

时间:2014-05-09 08:54:52

标签: ios mkmapview uigesturerecognizer mkannotationview

我选择MKAnnotationView之后有自定义视图,然后尝试添加这样的手势识别器:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)selectedAnnotationView {
    if([selectedAnnotationView.annotation isKindOfClass:[CustomPinAnnotation class]]) {
        CustomPinAnnotation *annotation = selectedAnnotationView.annotation;
        [selectedAnnotationView setCalloutOffset:CGPointMake(0,selectedAnnotationView.frame.size.height)];

        CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, selectedAnnotationView.frame.size.width, selectedAnnotationView.frame.size.height*2)];

    [selectedAnnotationView addSubview:calloutView];
    [UIView animateWithDuration:annotationAnimationTime animations:^{
        [calloutView setFrame:CGRectMake(-expandingAnnotationWidth/8, 0, [calloutView calculateWurstLenghtFromText:selectedAnnotationView.annotation.title], selectedAnnotationView.frame.size.height*2)];
    } completion:^(BOOL finished) {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)];
        [calloutView addGestureRecognizer:tapGesture];
        [calloutView setUserInteractionEnabled:YES];
    }];
}

在下面的屏幕中,您可以看到它是如何展开的,而不是: enter image description here

问题是手势识别器只能在标记区域上工作,而不能在整个标注上工作:

enter image description here

我可能知道发生了什么 - 我将子视图添加到MKAnnotation这是小的。但是如何解决这个问题?还有另一种绘制标注的方法,或者我可以扩展可点击区域。

2 个答案:

答案 0 :(得分:1)

1May你可以尝试改变这个:

CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, selectedAnnotationView.frame.size.width, selectedAnnotationView.frame.size.height*2)];

对此:

 CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

看看它是否有效,因为我觉得annotationView.frame太小了,并将你的calloutView设置为这个小区域。如果它是硬编码的,它可能会起作用。

答案 1 :(得分:0)

你的注释框架可能太小了。即使你将callout设置得足够大,它的父级注释本身也很小。有同样的问题,解决它真的很麻烦。

如果您不想弄乱帧和选定状态,并且还因为您已经使用了添加的自定义视图,为什么不执行以下操作:

  • 在选择注释时将自定义标注视图添加为全新/不同的注释/叠加(不像现在这样的子视图)
  • 取消选择注释时将其删除

您只需要正确定位它,甚至不需要手势识别器。