【问题标题】:Callout Annotation view is not clickable the center标注注释视图不可点击中心
【发布时间】:2013-10-18 09:59:40
【问题描述】:

我希望我的所有视图都是可点击的,而不仅仅是 leftCalloutAccessoryView 或 rightCalloutAccessoryView,我还希望中心也是可点击的

【问题讨论】:

  • 我在下面发布了我的答案.. 我已经有一段时间没有这样做了,所以如果有什么不清楚的地方请告诉我
  • 你试过我的答案了吗?我花了一段时间才把它放在一起。如果你至少告诉我它是否不适合你,我将不胜感激。除非希望找到由几行组成的答案

标签: ios objective-c mkmapview mkannotationview


【解决方案1】:

你可以使用,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint touchPoint = [touch locationInView:calloutView];

        BOOL isPointInsideView = [calloutView pointInside:touchPoint withEvent:nil];
        if (isPointInsideView)
        {
           // place your action code.  
        }

}

或者你可以使用,

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCalloutAction:)];
    tapGestureRecognizer.delegate = self;
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [calloutView addGestureRecognizer:tapGestureRecognizer];

-(void) tapCalloutAction:(id)sender
{
    // do stuff
}

【讨论】:

  • 但这可以让我点击地图上的任何地方,但我只想在你点击我按下识别我的白框时这样做,我正在使用此代码显示注释 MKAnnotationView * pin = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:@"current"];
  • 您可以触摸您的标注视图,[touch locationInView:yourAnnotationView.calloutView] 如果仅按此视图,则在此处执行操作。
  • MKAnnotationView * pin = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:@"current"]; CGPoint touchPoint = [touch locationInView:pin];我这样做了,所以它不起作用
  • calloutView 是什么?
【解决方案2】:

这个想法是你想让所有的“附件视图”都可以点击而不干扰实际注释的原始点击。我就是这样做的:

首先我创建并分配一个轻击手势到注释视图之后它已被选中(我在这里使用 obj-c 运行时对象关联..参见@ 987654321@要点):

// set up vars
static NSString *const kExtraTapGestureRecognizer = @"extraGesture";
UIControl *currentAnnotationControl;

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {      
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self 
                                           action:@selector(handleAnnotationViewTap:)];
    tap.numberOfTapsRequired = 1;
    [tap setInfo:kExtraTapGestureRecognizer];
    [view addGestureRecognizer:tap];

}

为了处理点击,我调用了MKMapViewDelegate 协议的mapView:annotationView:calloutAccessoryControlTapped:,这基本上意味着如果附件视图之间的视图被点击,就好像其中一个附件视图被点击了:

- (void)handleAnnotationViewTap:(UITapGestureRecognizer *)gestureRecognizer {
    MKAnnotationView *annotationView = (MKAnnotationView *)gestureRecognizer.view;
    // currentAnnotationControl is just a reference to one of the 
    // accessory views of the annotation that has just been selected.. 
    // see comment below
    [self mapView:self.mapView 
   annotationView:annotationView 
   calloutAccessoryControlTapped:currentAnnotationControl];
}

当 annotationView 返回时,我保存对其(左或右)附件视图之一的引用,如下所示:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[Random class]])
    {
        static NSString *annotationIdentifier = @"annotation";

        MKAnnotationView *annotationView =
        (MKAnnotationView *) [self.mapView annotationIdentifier];
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc]
                               initWithAnnotation:annotation reuseIdentifier:RiderAnnotationIdentifier];
            annotationView.canShowCallout = YES;

            UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            leftButton.frame = CGRectMake(0, 0,21, 21);
            [leftButton setImage:[UIImage imageNamed:@"smallInfo_rider_left.png"] forState:UIControlStateNormal];

            [leftButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
            annotationView.leftCalloutAccessoryView = leftButton;

            // this is where i store a reference to one of the accessory views
            currentAnnotationControl = leftButton;

            return annotationView;
        }
        else
        {
            annotationView.annotation = annotation;
        }

        return annotationView;

请记住,我们创建了一个额外的点击手势,我们必须在点击其中一个附件视图后立即删除它,如下所示:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    // we remove the extra tap gesture so that it doesn't interfere
    // with normal app flow in the future
    for (UIGestureRecognizer *gesture in [view gestureRecognizers]) {
        if ([[gesture info] isEqualToString:kExtraTapGestureRecognizer]) {
            [gesture removeTarget:nil action:NULL];
        }            
    }
    // more stuff

【讨论】:

  • 嘿伙计,你的回答没有给我解决方案,我肯定从你的例子中误解了一些东西,这不好
猜你喜欢
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多