问题描述
我有一个自定义UIView
,当用户单击MKMapView
上的自定义注释时,我将其显示为标注.
I have a custom UIView
that I am displaying as a callout when the user clicks on a custom annotation on an MKMapView
.
为此,我将MKAnnotationView
子类化,并按照此答案中的建议重载了-setSelected:selected animated:
方法.基本上,我将自定义视图添加为MKAnnotationView
子类的子视图.
To achieve this I have subclassed MKAnnotationView
and overloaded the -setSelected:selected animated:
method as suggested in this answer. Basically, I am adding my custom view as a subview of my MKAnnotationView
subclass.
问题是我根本无法与包含一个按钮和一个可滚动Web视图的标注进行交互.更重要的是,如果标注隐藏了注释,而我在该隐藏标注的大致位置处按标注,则标注将被关闭,并显示一个新标注.
The problem is that I can't interact with the callout, which contains a button and a scrollable webview, at all. What's more, if the callout hides an annotation and I press the callout at the approximate location of that hidden annotation, the callout will get dismissed and a new one will be shown.
// TPMapAnnotationView.m
@implementation TPMapAnnotationView
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
TPMapAnnotation* anno = ((TPMapAnnotation*)self.annotation);
QuickInfoView* qi = [[QuickTabView alloc] initWithFrame:CGRectMake(0, 0, 440, 300)];
[qi displayDataForAnnotation:anno];
[self addSubview:qi];
// some animiation code that doesn't change things either way
}
else
{
[[self.subviews objectAtIndex:0] removeFromSuperview];
}
}
下面的代码创建TPMapAnnotationView
.
// this is in the view controller that contains the MKMapView
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id) annotation
{
if ([annotation isKindOfClass:[TPMapAnnotation class]])
{
TPMapAnnotationView *customAnnotationView = (TPMapAnnotationView *)[myMap dequeueReusableAnnotationViewWithIdentifier:@"TPAnn"];
if (customAnnotationView == nil)
{
customAnnotationView = [[TPMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"TPAnn"];
}
[customAnnotationView setImage:annotationImage];
return customAnnotationView;
}
return nil; // blue radar circle for MKUserLocation class.
}
推荐答案
这是一个众所周知的问题.您在AnnotationView上添加的任何内容都不会检测到触摸.有一个很好的开源项目来解决这个问题. http://dev.tuyennguyen.ca/wp-content/uploads /2011/03/CustomMapAnnotationBlogPart1.zip , http: //dev.tuyennguyen.ca/wp-content/uploads/2011/03/CustomMapAnnotationBlogPart21.zip
This is a well known problem. Anything you add on AnnotationView will not detect touches. There is good open source project for this problem. http://dev.tuyennguyen.ca/wp-content/uploads/2011/03/CustomMapAnnotationBlogPart1.zip, http://dev.tuyennguyen.ca/wp-content/uploads/2011/03/CustomMapAnnotationBlogPart21.zip
EDIT:
是的.我还尝试将uibuttons添加到我自己的自定义注解视图中,但是后来偶然发现了这个项目,发现他的自定义注解视图实际上是一个注解.
Yes. I also tried hard to add uibuttons to my own custom annotationView but then I stumbled upon this project and found that his custom annotationView is actually a annotation.
无论如何,如果您想更改annotatioView的高度,则可以设置
Anyway if you want to to change height of annotatioView then you can set
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
calloutMapAnnotationView.contentHeight = height;
calloutMapAnnotationView.titleHeight = 25;
}
在这里,titleHeight是添加到CalloutMapAnnotationView的属性,该属性确定drawRectMethod中光泽"的高度
here, titleHeight is property added to CalloutMapAnnotationView which determines height of "gloss" in drawRectMethod
- (void)drawRect:(CGRect)rect {
glossRect.size.height = self.titleHeight;
}
如果您有任何困难,请告诉我.
if you are having any difficulty please let me know.
以及原始博客文章的链接: http://dev.tuyennguyen.ca/?p=298
And also the link to original blogpost:http://dev.tuyennguyen.ca/?p=298
这篇关于自定义MKMapView标注不响应触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!