- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 MKAnnotationView Frame 时遇到了问题。在提问之前我想澄清一些事情。
第一:
我有一个 MKMapView
和一些自定义注释:
看起来不错。
其次,我通过 MKPolylineView
在用户位置和此自定义注释之间画了一条线:
看起来不错
第三,然后我尝试通过自己的自定义更改默认注释图像:
这里我们有错误的结果,因为注释没有与线连接
第四,好吧,我试着改变这张图片的位置:
我刚刚向 MKAnnotationView
添加了一个新的 UIView
和我的 UIImageView
。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKAnnotationView *annotationView = nil;
annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if(!annotationView)
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:person reuseIdentifier:@"pin"];
else
annotationView.annotation = annotation;
UIImageView *avatarImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 25, 25)];
avatarImage.image = [UIImage imageNamed:avatarName];
[annotationView setImage:nil];
avatarImage.layer.masksToBounds = YES;
UIView *avatarView = [[UIView alloc] initWithFrame:CGRectMake(-7, 23, 30, 30)];
avatarImage.backgroundColor = [UIColor clearColor];
avatarView.layer.cornerRadius = 15.0f;
[avatarView addSubview:avatarImage];
[annotationView addSubview:avatarView];
return annotationView;
}
毕竟:
这就是我想要的:)
但有时当我点击此注释时,didSelectAnnotationView:
不会被调用。我知道为什么,因为我的 UIView
和来自 MKAnnotation
的图像。
我改变了MKAnnotation
的背景颜色:
当我点击这个红色区域时 didSelectAnnotationView:
被调用。
所以,问题是如何更改此 MKAnnotationView
的框架,以便我的 UIView
不会在外面?
附言我试过改变框架,不工作 :D
P.S.S 对不起我的英语:(
最佳答案
我在这里对自定义标注 View 有很长的解释 https://stackoverflow.com/a/19404994/1226370 .
有关触摸处理的所有详细信息都在链接的答案中进行了描述。
希望有用!
更新 1
在您将 MKAnnotationView 或 MKPinAnnotationView 子类化之后,您将实现如下两个方法:
- (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;
}
简而言之,在这些方法中,您指定是否应将当前位置的触摸视为 MKAnnotationView 上的触摸。
因此,如果您想增加注释的触摸区域,则必须对这些方法进行适当的更改。
关于ios - MKViewAnnotation 框架的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962642/
我在使用 MKAnnotationView Frame 时遇到了问题。在提问之前我想澄清一些事情。 第一: 我有一个 MKMapView 和一些自定义注释: 看起来不错。 其次,我通过 MKPolyl
我在 map 上放置了图钉,当我点击它们时,我调用了 didSelect。该函数仅在第一次点击引脚时被调用,之后它不会再次调用同一个引脚,除非我选择另一个引脚然后返回并点击它。 对我来说,这听起来像是
我是一名优秀的程序员,十分优秀!