關于給地圖大頭針加點擊事件

#pragma mark - MKMapViewDelegate

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation

{

NSString*identifer =@"SMAnonotationView";

SMAnonotationView*anonotationView = (SMAnonotationView*)[mapViewviewForAnnotation:annotation];

if(!anonotationView) {

anonotationView = [[SMAnonotationViewalloc]initWithAnnotation:annotationreuseIdentifier:identifer];

anonotationView.model=self.anonotionModel;

anonotationView.delegate=self;

}

anonotationView.delegateView=self;

returnanonotationView;

}

換上自定義大頭針后,發現其frame為0,無法點擊,之后思路

1.加frame,但位置不對達不到預期效果

2.-(void)mapView:(MKMapView*)mapView didSelectAnnotationView:(MKAnnotationView*)view

{

DebugLog(@"didSelectAnnotationView");

}

這個方法在系統大頭針會調用,而且一旦點擊將被選擇,有坑,躲開

3.- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event

{

UIView*fitView = [superhitTest:pointwithEvent:event];

//轉換坐標系

CGPointnewPoint = [self.anonotationViewconvertPoint:pointfromView:self];

//判斷觸摸點是否在view上

if(CGRectContainsPoint(self.anonotationView.bounds, newPoint)) {

fitView =self.anonotationView;

returnfitView;

}

returnnil;

}

此為正確方法,獲取超出super view的點擊事件。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容