![](/img/trans.png)
[英]How to trigger MKAnnotationView's callout view without touching the pin?
[英]How To add custom View in map's Annotation's Callout's
这是我的代码。 我想添加自己的自定义标注视图,而不是iOS默认视图。 我知道只有左侧标注和右侧标注视图,但是我需要添加带有自己的背景和标签的视图工具提示类型。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *userAnnotationView = nil;
if ([annotation isKindOfClass:MKUserLocation.class])
{
userAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"UserLocation"];
if (userAnnotationView == nil) {
userAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"UserLocation"];
}
else
userAnnotationView.annotation = annotation;
userAnnotationView.enabled = YES;
userAnnotationView.canShowCallout = YES;
userAnnotationView.image = [UIImage imageNamed:@"map_pin.png"];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,141,108)];
view.backgroundColor = [UIColor clearColor];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"toltip.png"]];
[view addSubview:imgView];
userAnnotationView.leftCalloutAccessoryView = view;
return userAnnotationView;
}
}
我遇到了同样的问题,最终做了以下事情:
当我收到mapView委托回调
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
(这是我想显示自定义CalloutView的时间)
我使用接收到的视图作为参数*(MKAnnotationView )view (这是一个引脚视图),并使用以下命令将我的自定义视图简单地添加到该引脚视图中
[view addSubview:customView];
它将在该图钉视图的顶部添加您的自定义视图,因此,如果我们希望它位于图钉之上,则必须像这样更改自定义视图中心属性:
CGRect customViewRect = customView.frame;
CGRect rect = CGRectMake(-customViewRect.size.width/2, -customViewRect.size.height-7, customViewRect.size.width, customViewRect.size.height);
customView.frame = rect;
[view addSubview:customView];
就我而言,它看起来像这样
注意 :
有一个警告,但是可以很容易地解决,您的自定义视图将忽略触摸事件 ,因为mapView处理触摸的方式有所不同。 快速解决方法 :
1)子类MKAnnotationView (或MKPinAnnotationView取决于您的需求)
2)在您的mapView委托中
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
使用您的子类而不是MKAnnotationView / MKPinAnnotationView
3)在您的子类.m文件中,重写以下两种方法:
- (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;
}
全部做完!
对于以上Swift中的最佳答案
这样可以节省几分钟来重写自己。
!注意:
有一个警告,但是可以很容易地解决,由于mapView对触摸的处理方式不同,您的自定义视图将忽略触摸事件。 快速解决方法:
1)子类MKAnnotationView(或MKPinAnnotationView取决于您的需求)
2)在您的mapView委托中
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
使用您的子类而不是MKAnnotationView / MKPinAnnotationView
3)在您的子类.m文件中,重写以下两种方法:
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, withEvent: event)
if (hitView != nil)
{
self.superview?.bringSubviewToFront(self)
}
return hitView;
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside = CGRectContainsPoint(rect, point)
if(!isInside) {
for view in self.subviews {
isInside = CGRectContainsPoint(view.frame, point)
break;
}
}
return isInside
}
我刚刚为地图视图创建了自定义标注,这样可以避免点击时标注消失的问题。 这是我采取的步骤的要点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.