ios - 检查用户是否在 map 中选择了另一个注释

标签 ios mkmapview mkannotation mkannotationview

我有一张带有注释的 map ,我正在执行以下操作:

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
[self methodA];}


 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
[self methodB];}

方法 A 和 B 都在 mapView.superview 中删除或添加 View 。

处理一个注释时一切正常。 问题是当我有多个注释并且我选择两个,一个接一个 .

如果我单击 map 中的其他任何地方,一切正常。但是当我在第一个注释 View 之后单击第二个注释 View 时,它执行“didDeselectAnnotationView”,然后执行“didSelectAnnotationView”,它同时调用方法 A 和 B,这不是我想要的。我希望它检测到我正在单击另一个注释并忽略这两种方法。

我对此进行了研究,但尚未找到任何解决方案。

我试图添加一个全局变量并使用它以及识别用户触摸的位置:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];

// Get the specific point that was touched
CGPoint point = [touch locationInView:self.mapView];

for (id<MKAnnotation> annotation in self.mapView.annotations) {
    MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
    if (anView){

        CGRect frame = CGRectMake(anView.frame.origin.x-5, anView.frame.origin.y-5, anView.frame.size.width+5, anView.frame.size.height+5);
        if ( CGRectContainsPoint(frame, point) ) {

            NSLog(@"BELONGS");
        }

    }

}}

但是,这并没有引起所有的注意,而且它有点像意大利面条式的解决方案。

任何想法如何解决这个问题?

祝一切顺利

最佳答案

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
    indexPathTag=aView.tag;
    [mapView deselectAnnotation:aView.annotation animated:YES];

}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView
{
}

从 didSelectAnnotationView 调用 deselectAnnotation。

关于ios - 检查用户是否在 map 中选择了另一个注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27244440/

相关文章:

ios - 检查 MKPolyline 是否与 MKPolygon 相交

ios - 符合 MKAnnotation 协议(protocol)的类的自定义方法

ios - 使用 MKAnnotation 继续下一个 View Controller

ios - 在 iOS 13 上的 UIScrollView 中设置 UIRefreshControl

ios - fetchedResultsController 返回 nil

ios - TableView 中的 UICollectionView 不填充单元格?

ios - 选择 AnnotationView 两次

ios - 如何从数据库值创建 cupertinoactionsheet

ios - MKAnnotationView旋转

ios4 - 如何在 MKMapView 中显示两种不同颜色的引脚注释?