我的 map View 上有两种不同类别的注释。当点击每种类型的注释时,会出现不同的标注。当第一种注释标注被点击时,它会将我的用户带到另一个屏幕。但是,当点击第二种类型时,我想要显示一个警报。
我该如何执行这个?我只是不确定我应该把 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];
}
最佳答案
您不需要使用 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
}
}
关于ios - Obj-c - 为同一 map View 上的两个不同注释设置两个不同的标注操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526360/