【问题标题】:Drag/pan gestures on a GMSMapView not getting captured after update to SDK 1.3.1更新到 SDK 1.3.1 后未捕获 GMSMapView 上的拖动/平移手势
【发布时间】:2013-07-02 00:43:08
【问题描述】:

我在通过手势识别器在 GMSMapView 上捕获拖动/平移手势时遇到了一个奇怪的问题。此问题仅在从 GMS 1.2 更新到 1.3.1 后才出现,其中(引用 documentation),

GMSMapView 更积极地消耗触摸

我有一个 UIViewController 在其主视图下持有一个 GMSMapView。我发现 GMSMapDelegate 没有提供处理拖动/平移手势的方法,所以我在 UIViewController 中添加了一个 UIPanGestureRecognizer,将其链接到 IBAction 选择器,并设置引用插座和插座集合,根据此处链接的屏幕截图:http://i.stack.imgur.com/gktoa.png

因此,任何拖动操作都会触发recognizeDragOnMap: 选择器,如下所示:

-(IBAction)recognizeDragOnMap:(id)sender {
    NSLog(@"recognizeDragOnMap");

    UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)sender;
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        NSLog(@"Still dragging");
        return;
    }
    NSLog(@"DragEnded");

    GMSCameraPosition *position;

    if ((position = self.mapView.camera)) {
        self.automaticCameraPositionChange = NO;
        CLLocationCoordinate2D coordinate = [position targetAsCoordinate];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        [self.origin dragPinToLocation:location];
    } else {
        NSLog(@"No map camera");
    }
}

此设置曾经在 GMS 1.2.0 下完美运行。更新后,GMSMapView 会像以前一样响应手势,但是上面的方法永远不会被调用!

任何人都知道发生了什么和/或如何解决它?

【问题讨论】:

  • 非常偏执,我做了以下测试: 1) 在另一个没有 GMSMapView 的 UIViewController 上设置了一个等效的 UIPanGestureRecognizer ...你瞧,工作顺利。 2) 将我的项目恢复到 1.3.1 之前的提交,也可以顺利运行。
  • 另外——我知道mapView: didChangeCameraPosition:,但这还不够:我需要能够区分由地图上的手势和其他操作生成的相机更新。

标签: ios cocoa google-maps-sdk-ios xcode-storyboard


【解决方案1】:

对于 1.4 或更高版本,您只需在 GMSUISettings 对象中设置 consumesGesturesInView = NO

如果您这样做,请注意,当您只想与地图交互时,您将不得不处理可能使您的超级视图执行某些操作的事件...我的意思是,例如,拖动 GMSMapView添加到滚动视图将在拖动时滚动滚动视图!

【讨论】:

    【解决方案2】:
    mapView.settings.consumesGesturesInView = YES;
    

    也很有帮助。我的父母视图正在消耗我的手势识别器

    加上

    for (UIGestureRecognizer *gestureRecognizer in mapView.gestureRecognizers) {
        [gestureRecognizer addTarget:self action:@selector(handlePan:)];
    }
    
    
    //////
    
    
    
    -(IBAction) handlePan:(UIPanGestureRecognizer*)sender {
    
    
    
        if (sender.state == UIGestureRecognizerStateEnded) {
            CGSize size = mapView.frame.size;
            CGPoint tp = CGPointMake(size.width/2, size.height/2);
            CLLocationCoordinate2D loc = [mapView.projection coordinateForPoint:tp];
            [mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:loc zoom:mapView.camera.zoom]];
    
    
    
        }
    
    
    }
    

    太棒了!而且您甚至不需要添加自己的平移手势识别器

    【讨论】:

    • GAH 就是这样。我被卡住了,把头发扯掉了!
    【解决方案3】:

    As it turns outGMSMapView 实例现在拥有一个 GMSBlockingGestureRecognizer,它吞噬了所有手势。所以有两个选择:

    • 在加载GMSMapView后删除这个识别器(可能会破坏依赖它的内部功能)(like this);或
    • 将我自己的目标/动作附加到识别器。

    使用第二种方法,UIViewControllerviewDidLoad 中的以下代码使事情恢复正常:

    self.mapView = (RAMapView *)[self.view viewWithTag:1];
    
    for (UIGestureRecognizer *gestureRecognizer in self.mapView.gestureRecognizers) {
        [gestureRecognizer addTarget:self action:@selector(recognizeDragOnMap:)];
    }
    

    老实说,这是一个丑陋、邪恶的组合,但它确实有效。 :)

    【讨论】:

      【解决方案4】:

      更好的选择是设置您自己的手势识别器的委托,然后添加委托方法,如下所示:

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

      它会像以前一样工作。

      【讨论】:

      • 嘿 Ryan,我确实尝试过,但没有效果。似乎 GMSBlockingGestureRecognizer 真的具有侵略性!
      【解决方案5】:

      嘿,Google 地图中有一个委托方法。

      - (void)mapView:(GMSMapView *)mapView    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;
      

      【讨论】:

        【解决方案6】:

        通过google api和其他方法分析,我没有找到合适的。这个问题的最佳答案是使用平移手势。

        在mapView中添加平移手势

        self.mapView.settings.consumesGesturesInView = false
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self. panHandler(_:)))
        self.mapView.addGestureRecognizer(panGesture)
        

        平移手势方法as的实现

        @objc private func panHandler(_ pan : UIPanGestureRecognizer){
        
                if pan.state == .ended{
                    let mapSize = self.mapView.frame.size
                    let point = CGPoint(x: mapSize.width/2, y: mapSize.height/2)
                    let newCoordinate = self.mapView.projection.coordinate(for: point)
                    print(newCoordinate)
                     //do task here
                }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多