我希望我的所有视图都是可点击的,而不只是leftCalloutAccessoryView
或r ightCalloutAccessoryView
,我也希望中心也可以点播
答案 0 :(得分: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
}
答案 1 :(得分:1)
这个想法是你想让所有的“附件视图”都可以点亮而不会干扰实际注释的原始点击..这就是我的工作方式:
首先我在之后创建并为注释视图分配了一个轻击手势,就像这样(我在这里使用obj-c运行时对象关联..请参阅{ {3}}要点:
// 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
协议this,这基本上意味着如果配件视图之间的视图被点击,就好像其中一个配件视图被点击了:< / p>
- (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