对象-在同一mapView上为两个不同的注解设置两个不同的标注动作?

时间:2018-09-26 21:38:36

标签: ios objective-c mapkit

我的mapView上有两种不同的注释类别。轻按每种类型的注释时,将显示不同的标注。点击第一种类型的注释标注时,它将带我的用户到另一个屏幕。但是,当点击第二种类型时,我希望显示警报。

如何执行此操作?我只是不确定应该将TapGesture放在哪里calloutTappedThree。到目前为止,请参见下面的代码:

MapViewController.m

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    if([annotation isKindOfClass:[meetupAnn class]]) {
        static NSString *identifier = @"currentLocation";
        MKAnnotationView *pulsingView = (MKAnnotationView *)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

            pulsingView.canShowCallout = YES;

            pulsingView.image = [UIImage imageNamed:@"meetupbeacon.png"];
            UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meetupbeacon.png"]];
            pulsingView.leftCalloutAccessoryView = iconView;
        }

        return pulsingView;
    }


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {

        MKAnnotationView *pinView = (MKAnnotationView*)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
        if (!pinView)
        {

            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];

            pinView.image = [UIImage imageNamed:@"mapann3.png"];

        } else {
            pinView.annotation = annotation;
        }
        pinView.canShowCallout = YES;
        pinView.calloutOffset = CGPointMake(0, 0);

        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapann3.png"]];
        pinView.leftCalloutAccessoryView = iconView;

        return pinView;
    }
    return nil;

}



- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {



        UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTappedTwo:)];
        [view addGestureRecognizer:tapGesture2];



    }

-(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
{
    NSLog(@"CALL OUT TWO TAPPED");

    MKAnnotationView *view = (MKAnnotationView*)sender.view;

    id <MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];

        NSMutableDictionary *dictionary = self.friendData[selectedPoint.index];
        yourViewController.frienduserData = dictionary;

        [self.navigationController pushViewController:yourViewController animated:YES];

    }

}


-(void)calloutTappedThree:(UITapGestureRecognizer *) sender
{

          NSLog(@"Callout Three was tapped");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"RSVP for Meet-Up?"
                                                    message:@"Test Alert!"
                                                   delegate:self
                                          cancelButtonTitle:@"Join"
                                          otherButtonTitles:@"Maybe Next Time...",nil];
    [alert show];


}

1 个答案:

答案 0 :(得分:1)

您不需要使用TapGesture。使用didSelectAnnotationView方法。

例如

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    if ([view.annotation isKindOfClass:[meetupAnn class]]) {
        // Call calloutTappedTwo
    } else if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {
        // Call calloutTappedThree
    }
}