我製作了自定義地圖標註。我的標註包含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:
我剛剛用durty hack實現了我的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
而且使用該類別找到標註在視圖層次:
@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,我用我自己 - 看問題更新。 – k06a
這是錯誤的... 在你的代碼中,你添加了你的cutomCallOutView下的針圖像。 您無法直接在didSelectAnnotationView方法中添加自定義視圖。 看我的例子, 我稱之爲didSelectAnnotationView另一種方法命名爲「findCallOut」 這種方法循環的自我和Digg所有「的MapView」,直到發現當前打開callOutView並改變它的性質和finaly添加所需的自定義視圖作爲一個子視圖。 如果你隱藏默認的氣泡圖像和標籤等。 找到後UICallOutView運行在另一個for循環中找到隱藏的子視圖。 – ytur
我正在使用單個自定義標註視圖。我正在嘗試將其添加到用戶位置點。在'didSelectAnnotationView'內部,我將標註視圖添加到註釋視圖,並在'didDeselectAnnotationView'內添加了標註視圖。現在我正在嘗試更改註記視圖框架並移動內層以保留其位置......我遇到了一些問題,但是可以使用我的自定義標註視圖。似乎問題在標註視圖框架超出父母框架.... – k06a