我已经制作了自定义地图标注。我的标注包含
我像这里一样实现了
这是我的
UIButtons
和UITextView
。 当我点击UIButton
时,它会很好地按下。 但是当我点击UITextView
时,它会将光标移动到点击位置,然后取消选择引脚并隐藏标注...我像这里一样实现了
MyAnnotationView
的hitTest:withEvent:
方法:https://stackoverflow.com/a/13495795/440168
但是我在日志中看到,[super hitTest:withEvent:]
从未返回nil
。这是我的
MyAnnotationView
方法:- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isInside = [super pointInside:point withEvent:event];
if (isInside)
return YES;
for (UIView * subview in self.subviews)
{
if ([subview isKindOfClass:[NSClassFromString(@"UICalloutView") class]])
continue;
CGPoint inPoint = [self convertPoint:point toView:subview];
BOOL isInside = [subview pointInside:inPoint withEvent:nil];
if (isInside)
return YES;
}
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView * hitView = [super hitTest:point withEvent:event];
if (hitView)
return hitView;
for (UIView * subview in self.subviews)
{
if ([subview isKindOfClass:[NSClassFromString(@"UICalloutView") class]])
continue;
CGPoint inPoint = [self convertPoint:point toView:subview];
hitView = [subview hitTest:inPoint withEvent:event];
if (hitView)
return hitView;
}
return nil;
}
更新1:
以下是我添加自定义标注视图的代码:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
for (UIView * subview in view.subviews)
subview.hidden = YES;
[view addSubview:self.myCalloutView];
self.myCalloutView.center = CGPointMake(view.bounds.size.width/2,-self.myCalloutView.bounds.size.height/2);
// ...
}
更新2:
我刚刚使用了一些小技巧实现了我的MKMapView子类。但是这样可以工作!
@implementation HNPMapView
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
for (UIView * v in [self findSubviewsOfClass:[MyCallout class]]) {
CGPoint point = [recognizer locationInView:v];
if (CGRectContainsPoint(v.bounds, point))
return;
}
//[super performSelector:@selector(handleTap:) withObject:recognizer];
void (*functionPointer)(id,SEL,...) = [MKMapView instanceMethodForSelector:@selector(handleTap:)];
functionPointer(self,@selector(handleTap:),recognizer);
}
@end
使用此类别在视图层次结构中查找Callout:
@interface UIView (FindSubview)
- (NSArray *)findSubviewsOfClass:(Class)class;
@end
@implementation UIView (FindSubview)
- (NSArray *)findSubviewsOfClass:(Class)class
{
NSMutableArray * found = [NSMutableArray array];
for (UIView * subview in self.subviews)
{
if ([subview isKindOfClass:class])
[found addObject:subview];
[found addObjectsFromArray:[subview findSubviewsOfClass:class]];
}
return found;
}
@end
for
循环上设置断点,但它从未捕获...我没有使用UICalloutView,我正在使用自己的 - 请查看问题更新。 - k06adidSelectAnnotationView
中,我将弹出视图添加到注释视图中,在didDeselectAnnotationView
中,我从父视图中删除弹出视图。现在我正在尝试更改注释视图框架并移动内部层以保持固定在其位置...我遇到了一些问题,但我可以点击我的自定义弹出视图。似乎问题在于弹出视图框架超出了父框架... - k06a