MKAnnotationView子视图

目前,我在实现具有多个自定义UIImageViews的自定义MKAnnotationView项目中遇到问题。 所以这些自定义的UIImageView有一个清晰的button,不必添加手势识别器。

在这里输入图像说明

正如你所看到的,实际上点击MKAnnotationView子视图并且发生一些动作是有益的。

我为MKAnnotationView实现了一个协议,其中MKAnnotationView中的每个图像子视图对MKMapView的所有者控制器进行callback…下面是代码…

PHProfileImageView *image = [[PHProfileImageView alloc] initWithFrame:CGRectMake(newX - radius / 5.0f, newY - radius / 5.0f, width, height)]; [image setFile:[object objectForKey:kPHEventPictureKey]]; [image.layer setCornerRadius:image.frame.size.height/2]; [image.layer setBorderColor:[[UIColor whiteColor] CGColor]]; [image.layer setBorderWidth:2.0f]; [image.layer setMasksToBounds:YES]; [image.profileButton setTag:i]; [image.profileButton addTarget:self action:@selector(didTapEvent:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:image]; - (void)didTapEvent:(UIButton *)button { NSLog(@"%@", [self.pins objectAtIndex:button.tag]); if (self.delegate && [self.delegate respondsToSelector:@selector(didTapEvent:)]) { [self.delegate JSClusterAnnotationView:self didTapEvent:[self.pins objectAtIndex:button.tag]]; } } 

所以,你可以看到,我已经尝试logging点击图像的结果,但没有:(是我的方式执行这不是去的路?我应该有CAShapeLayers或什么?不知道在这个点,任何人有任何想法?

编辑

我想我可能不得不实现一个自定义标注视图。 由于标注视图实际上将button添加到其视图,并可以响应触摸事件…不完全确定,因为只有在注释视图被点击后才会显示标注。 在这种情况下,实际注释视图是中间标签

所以我调整了mkannotationview的框架到一个更大的框架,显然所有的子视图实际上不在MKAnnotationView的范围内,所以子视图实际上并没有被挖掘。 现在,我想到这个解决scheme,这可能不是最好的解决scheme。

如果任何人有任何build议,而不是添加到MKAnnotationView的子视图来创build我目前的视图,这将是伟大的!

对于具有可点击button的自定义AnnotationView,您必须在项目中创build自定义的AnnotationView子类。 为此创build一个新的文件。

在这里输入图像说明

并将这两个方法添加到实现文件中。

 - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event { UIView* hitView = [super hitTest:point withEvent:event]; if (hitView != nil) { [self.superview bringSubviewToFront:self]; } return hitView; } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event { CGRect rect = self.bounds; BOOL isInside = CGRectContainsPoint(rect, point); if(!isInside) { for (UIView *view in self.subviews) { isInside = CGRectContainsPoint(view.frame, point); if(isInside) break; } } return isInside; } 

然后再次转到ViewController.m文件,并修改viewDidLoad方法。

 - (void)viewDidLoad { [super viewDidLoad]; self.mapKit.delegate = self; //Set Default location to zoom CLLocationCoordinate2D noLocation = CLLocationCoordinate2DMake(51.900708, -2.083160); //Create the CLLocation from user cordinates MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 50000, 50000); //Set zooming level MKCoordinateRegion adjustedRegion = [self.mapKit regionThatFits:viewRegion]; //add location to map [self.mapKit setRegion:adjustedRegion animated:YES]; // create animation zooming // Place Annotation Point MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init]; //Setting Sample location Annotation [annotation1 setCoordinate:CLLocationCoordinate2DMake(51.900708, -2.083160)]; //Add cordinates [self.mapKit addAnnotation:annotation1]; } 

现在将该自定义视图添加到ViewController.xib。

现在创build这个委托方法如下。

 #pragma mark : MKMapKit Delegate -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { AnnotationView *pinView = nil; //create MKAnnotationView Property static NSString *defaultPinID = @"com.invasivecode.pin"; //Get the ID to change the pin pinView = (AnnotationView *)[self.mapKit dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; //Setting custom MKAnnotationView to the ID if ( pinView == nil ) pinView = [[AnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID]; // init pinView with ID [pinView addSubview:self.customView]; addSubview:self.customView.center = CGPointMake(self.customView.bounds.size.width*0.1f, -self.customView.bounds.size.height*0.5f); pinView.image = [UIImage imageNamed:@"Pin"]; //Set the image to pinView return pinView; } 

几个月前,我也从Stackoverflow上发布的人那里得到了这个答案。 我将其修改为我想要的项目。 希望这会做你的工作。