【问题标题】:How to capture Tap gesture on MKMapView如何在 MKMapView 上捕获点击手势
【发布时间】:2011-05-18 02:20:39
【问题描述】:

我正在尝试在我的MKMapView 上捕获点击事件,这样我可以在用户点击的位置放置一个MKPinAnnotation。基本上我有一张覆盖有MKOverlayViews(显示建筑物的覆盖)的地图,当用户点击它时,我想通过删除MKPinAnnotaion 并在标注中显示更多信息来为用户提供有关该覆盖的更多信息。 谢谢你。

【问题讨论】:

    标签: ios4 mkmapview uitapgesturerecognizer mkoverlay mkpinannotationview


    【解决方案1】:

    出于某种原因,UIGestureRecognizer 在 Swift 中对我不起作用。当我使用 UIGestureRecognizer 方式时。当我使用 touchesEnded 方法时,它返回一个 MKNewAnnotationContainerView。似乎这个 MKNewAnnotationContainerView 阻止了我的 MKMapView。幸运的是,它是 MKMapView 的子视图。所以我循环遍历 MKNewAnnotationContainerView 的 superviews 直到 self.view 来获取 MKMapView。我设法通过点击固定mapView。

    斯威夫特 4.1

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let t = touches.first
        print(t?.location(in: self.view) as Any)
        print(t?.view?.superview?.superview.self as Any)
        print(mapView.self as Any)
        var tempView = t?.view
        while tempView != self.view {
            if tempView != mapView {
                tempView = tempView?.superview!
            }else if tempView == mapView{
                break
            }
    
        }
      let convertedCoor = mapView.convert((t?.location(in: mapView))!, toCoordinateFrom: mapView)
       let pin =  MKPointAnnotation()
        pin.coordinate = convertedCoor
        mapView.addAnnotation(pin)
    }
    

    【讨论】:

      【解决方案2】:

      如果您想在地图视图中单击/点击,这是我正在使用的代码的 sn-p。 (可可和斯威夫特)

      let gr = NSClickGestureRecognizer(target: self, action: "createPoint:")
      gr.numberOfClicksRequired = 1
      gr.delaysPrimaryMouseButtonEvents = false // allows +/- button press
      gr.delegate = self
      map.addGestureRecognizer(gr)
      

      在手势委托方法中,一个更喜欢双击手势的简单测试……

      func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
        let other = otherGestureRecognizer as? NSClickGestureRecognizer
        if (other?.numberOfClicksRequired > 1) {
          return true; // allows double click
        }
      
        return false
      }
      

      如果您希望地图处于各种“状态”,您还可以在其他委托方法中过滤手势,其中一种允许单击/单击

      【讨论】:

        【解决方案3】:

        我在 viewDidLoad: 中设置了长按 (UILongPressGestureRecognizer),但它只检测到第一次触摸时的唯一一次触摸。

        我在哪里可以设置长按来检测所有触摸?(这意味着每次等待用户触摸屏幕以按下图钉时地图准备就绪)

        viewDidLoad: 方法!

        - (void)viewDidLoad {
            [super viewDidLoad];mapView.mapType = MKMapTypeStandard;
        
            UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
            [self.mapView addGestureRecognizer:longPressGesture];
            [longPressGesture release];
        
            mapAnnotations = [[NSMutableArray alloc] init];
            MyLocation *location = [[MyLocation alloc] init];
            [mapAnnotations addObject:location];
        
            [self gotoLocation];
            [self.mapView addAnnotations:self.mapAnnotations];
        }
        

        handleLongPressGesture 方法:

        -(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
            // This is important if you only want to receive one tap and hold event
            if (sender.state == UIGestureRecognizerStateEnded)
            {NSLog(@"Released!");
                [self.mapView removeGestureRecognizer:sender];
            }
            else
            {
                // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
                CGPoint point = [sender locationInView:self.mapView];
                CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
                // Then all you have to do is create the annotation and add it to the map
                MyLocation *dropPin = [[MyLocation alloc] init];
                dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
                dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
        //        [self.mapView addAnnotation:dropPin];
                [mapAnnotations addObject:dropPin];
                [dropPin release];
                NSLog(@"Hold!!");
                NSLog(@"Count: %d", [mapAnnotations count]);
            }   
        }
        

        【讨论】:

          【解决方案4】:

          您可以使用UIGestureRecognizer 来检测地图视图上的触摸。

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

          在您设置地图视图的位置(例如viewDidLoad),将手势识别器附加到地图视图:

          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];
          }
          

          【讨论】:

          • 感谢您的建议,一旦我成功了,我会尽快回复。我确实尝试过单击,但之后我无法显示我的 PinAnnotations 的标注。看起来我需要使用 LongPressureGesture
          • UITapGestureRecognizer 在 iOS 6 中不再识别 MKMapView。它在 iOS 5 中有效。关于这个问题有什么想法吗?
          • @phix23,尝试实现shouldRecognizeSimultaneouslyWithGestureRecognizer 并从那里返回YES。在为要调用的 shoudRecognize 委托方法添加 GR 之前,需要执行 tgr.delegate = self;
          • 对“法律”链接有什么想法(不确定在提出这个问题时是否存在)?我想让它表现得像平常一样,我想要比迭代地图视图的子视图更聪明的东西来查看触摸是否在标签内或其他任何东西。
          • 添加 UIPanGestureRecognizer 来处理缩放(捏)和移动(平移)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多