1

我希望我的所有视图都是可点击的,而不仅仅是leftCalloutAccessoryViewor r ightCalloutAccessoryView,我也希望中心也是可点击的

在此处输入图像描述

4

2 回答 2

2

您可以使用,

- (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
}
于 2013-10-18T10:11:58.520 回答
1

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

首先,我在像这样选择注释视图创建并分配一个轻击手势(我在这里使用 obj-c 运行时对象关联.. 参见这个要点):

// 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
于 2013-10-18T10:31:49.550 回答