它的iOS标注附件窃听区分 [英] iOS distinguish between which callout accessory is tapped
本文介绍了它的iOS标注附件窃听区分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的地图注解,我有一个UIButton如标注每个配件视图。在 - (空)的MapView:(的MKMapView *)的MapView annotationView:(MKAnnotationView *)查看calloutAccessoryControlTapped:(UIControl *)控制
方法,我怎么找出哪些附件视图被感动了处理每个事件?这里是我的code:
- (MKAnnotationView *)的MapView:(*的MKMapView)的MapView viewForAnnotation:(ID< MKAnnotation>){注解如果([注解isKindOfClass:[MKUserLocation类])
回零;MKPinAnnotationView * MyPin = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:@当前];*的UIButton = calloutButton [UIButton的buttonWithType:UIButtonTypeDetailDisclosure];
*的UIButton = directionsButton [UIButton的buttonWithType:UIButtonTypeCustom];
directionsButton.frame = CGRectMake(0,0,23,23);
[directionsButton了setBackgroundImage:[UIImage的imageNamed:@directions.png] forState:UIControlStateNormal];MyPin.leftCalloutAccessoryView = directionsButton;
MyPin.rightCalloutAccessoryView = calloutButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop = YES;
MyPin.canShowCallout = YES;
MyPin.pinColor = MKPinAnnotationColorRed;返回MyPin;
} - (无效)MapView类:(*的MKMapView)的MapView annotationView:(MKAnnotationView *)查看calloutAccessoryControlTapped:(UIControl *){控制注释*安= view.annotation;如果([控件标记] == 1){ CLLocationCoordinate2D currentCoords = {ann.coordinate.latitude,ann.coordinate.longitude}; MKPlacemark *地点= [[MKPlacemark页头] initWithCoordinate:currentCoords addressDictionary:无];
MKMapItem *目的地= [[MKMapItem页头] initWithPlacemark:地方];
destination.name = ann.title;
destination.url = [NSURL URLWithString:@http://www.wccca.com/PITS];
NSArray的*项目= [[NSArray的页头] initWithObjects:目的地,无]
*的NSDictionary选项= [[NSDictionary的页头] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey,零]
[MKMapItem openMapsWithItems:项目launchOptions:选择];}如果([控制变量] == 2){ MKCoordinateRegion区域;
region.center.latitude = ann.coordinate.latitude;
region.center.longitude = ann.coordinate.longitude;
region.span.latitudeDelta = 0.02;
region.span.longitudeDelta = 0.02; [self.mapView setRegion:地区动画:是];
}}
解决方案
而不是设置和使用标签,你可以只检查是否控制
是左或右附件查看
如果(控制== view.leftCalloutAccessoryView){
//处理左控制水龙头...
}
其他
如果(控制== view.rightCalloutAccessoryView){
//处理右控制水龙头...
}
In my map annotations, I have a UIButton as each accessory view in the callouts. In the - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
method, how do I figure out which accessory view was touched to handle each the events? Here's my code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
UIButton *calloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIButton *directionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
directionsButton.frame = CGRectMake(0, 0, 23, 23);
[directionsButton setBackgroundImage:[UIImage imageNamed:@"directions.png"] forState:UIControlStateNormal];
MyPin.leftCalloutAccessoryView = directionsButton;
MyPin.rightCalloutAccessoryView = calloutButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop= YES;
MyPin.canShowCallout = YES;
MyPin.pinColor = MKPinAnnotationColorRed;
return MyPin;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
Annotation *ann = view.annotation;
if ([control tag] == 1) {
CLLocationCoordinate2D currentCoords = {ann.coordinate.latitude, ann.coordinate.longitude};
MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate: currentCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark: place];
destination.name = ann.title;
destination.url = [NSURL URLWithString:@"http://www.wccca.com/PITS"];
NSArray *items = [[NSArray alloc] initWithObjects: destination, nil];
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems: items launchOptions: options];
}
if ([control tag] == 2) {
MKCoordinateRegion region;
region.center.latitude = ann.coordinate.latitude;
region.center.longitude = ann.coordinate.longitude;
region.span.latitudeDelta = 0.02;
region.span.longitudeDelta = 0.02;
[self.mapView setRegion:region animated:YES];
}
}
解决方案
Rather than setting and using tags, you could just check if control
is the left or right accessory view:
if (control == view.leftCalloutAccessoryView) {
//handle left control tap...
}
else
if (control == view.rightCalloutAccessoryView) {
//handle right control tap...
}
这篇关于它的iOS标注附件窃听区分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文