gpt4 book ai didi

ios4 - 如何在MKMapView上捕获轻击手势

转载 作者:行者123 更新时间:2023-12-03 08:53:24 25 4
gpt4 key购买 nike

我试图捕获MKMapView上的tap事件,这样我可以在用户点击的位置放一个MKPinAnnotation。基本上,我的 map 上覆盖了MKOverlayViews(显示建筑物的覆盖层),当用户点击该覆盖层时,我想通过放下MKPinAnnotaion并在标注中显示更多信息来向其提供有关该覆盖层的更多信息。
谢谢。

最佳答案

您可以使用UIGestureRecognizer来检测 map View 上的触摸。

但是,我建议不要双击,而要双击(UITapGestureRecognizer)或长按(UILongPressGestureRecognizer)。单击可能会干扰用户尝试单击图钉或标注本身的情况。

在设置 map View 的地方(例如,在viewDidLoad中),将手势识别器附加到 map View :

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];

或长按:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];

handleGesture:方法中:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;

CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
}

关于ios4 - 如何在MKMapView上捕获轻击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317810/

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