簡體   English   中英

Callout Annotation視圖不能在中心點擊

[英]Callout Annotation view is not clickable the center

我希望我的所有視圖都是可點擊的,而不僅僅是leftCalloutAccessoryView或r ightCalloutAccessoryView ,我也希望中心也可以點播

在此輸入圖像描述

您可以使用,

- (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
}

這個想法是你想讓所有“附件視圖”都可以在干擾實際注釋的原始點擊的情況下進行操作。這就是我的工作方式:

首先,我在選擇它之后創建並為注釋視圖分配一個輕擊手勢(我在這里使用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 Protocol的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM