2013-07-03 59 views
3

我製作了自定義地圖標註。我的標註包含UIButtonsUITextView。當我點擊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 

回答

1

中則hitTest方法,你應該創建一個虛擬矩形根據你的對象是可觸摸定義觸摸區域。

在UICalloutView中,您應該使用另一個循環來查找您的UITextView。

之後,您應該根據您的UITextView尺寸和UICalloutView視圖原點定義此虛擬矩形。

你也不需要在for循環中使用continue語句, 當返回時,所有的循環都會立即停止。

所以你則hitTest方法應該是這樣的,

- (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]]) { 

      for(UIView *subTextView in subview.subviews){ 
       if([subTextView isKindOfClass:[UITextView class]]){ 

        CGRect touchableArea = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, subTextView.frame.size.width, subTextView.frame.size.height); 
        if (CGRectContainsPoint(touchableArea, point)){ 
         return subTextView; 
        } 
       }    
      }    
     } 
    }  
    return nil; 
} 
+0

我設置的'for'循環斷點,但它從來沒有抓到......我不使用UICalloutView,我用我自己 - 看問題更新。 – k06a

+0

這是錯誤的... 在你的代碼中,你添加了你的cutomCallOutView下的針圖像。 您無法直接在didSelectAnnotationView方法中添加自定義視圖。 看我的例子, 我稱之爲didSelectAnnotationView另一種方法命名爲「findCallOut」 這種方法循環的自我和Digg所有「的MapView」,直到發現當前打開callOutView並改變它的性質和finaly添加所需的自定義視圖作爲一個子視圖。 如果你隱藏默認的氣泡圖像和標籤等。 找到後UICallOutView運行在另一個for循環中找到隱藏的子視圖。 – ytur

+0

我正在使用單個自定義標註視圖。我正在嘗試將其添加到用戶位置點。在'didSelectAnnotationView'內部,我將標註視圖添加到註釋視圖,並在'didDeselectAnnotationView'內添加了標註視圖。現在我正在嘗試更改註記視圖框架並移動內層以保留其位置......我遇到了一些問題,但是可以使用我的自定義標註視圖。似乎問題在標註視圖框架超出父母框架.... – k06a

相關問題