gpt4 book ai didi

iphone - 我怎样才能捕捉到 MapView 上的点击,然后将它传递给默认的手势识别器?

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:33 26 4
gpt4 key购买 nike

这就是我想要的 - 用户在 map 上点击,我的代码被执行,然后系统代码被执行(如果用户点击了注释标注等...)。

我在 map View 中添加了简单的点击识别器:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapViewTapped:)];
[self.mapView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

在 mapViewTapped 中,我的代码被执行。现在我想通知点击的系统代码(例如显示标注)。我怎么做?如何传递我拦截的事件?

最佳答案

一种方法是实现 UIGestureRecognizerDelegate 方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 并在其中返回 YES:

//add <UIGestureRecognizerDelegate> to .h to avoid compiler warning

//add this where you create tapGestureRecognizer...
tapGestureRecognizer.delegate = self;

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

现在您的 mapViewTapped: 将被调用,然后 map View 的识别器将调用其方法。如果点击是在注释 View 上,则 map View 将显示其标注(如果您已实现,将调用 didSelectAnnotationView 委托(delegate)方法)。


另一种方式,如果你需要更多的控制,那么在你的 mapViewTapped: 中,你可以检查点击是否在注释 View 上,然后手动选择注释,然后将显示其标注(并调用 didSelectAnnotationView 委托(delegate)方法):

-(void)mapViewTapped:(UITapGestureRecognizer *)tgr
{
CGPoint p = [tgr locationInView:mapView];

UIView *v = [mapView hitTest:p withEvent:nil];

id<MKAnnotation> ann = nil;

if ([v isKindOfClass:[MKAnnotationView class]])
{
//annotation view was tapped, select it...
ann = ((MKAnnotationView *)v).annotation;
[mapView selectAnnotation:ann animated:YES];
}
else
{
//annotation view was not tapped, deselect if some ann is selected...
if (mapView.selectedAnnotations.count != 0)
{
ann = [mapView.selectedAnnotations objectAtIndex:0];
[mapView deselectAnnotation:ann animated:YES];
}
}
}

关于iphone - 我怎样才能捕捉到 MapView 上的点击,然后将它传递给默认的手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com