【问题标题】:UIPanGestureRecognizer on MKMapView?MKMapView 上的 UIPanGestureRecognizer?
【发布时间】:2011-06-27 16:42:33
【问题描述】:

当用户使用地图视图 i 移动时,我想添加一些逻辑。 e.他做了一个平底锅触摸。但是当我添加手势识别器并且我想记录触摸时,什么也没有发生。当我在另一个视图控制器中尝试它并将识别器添加到控制器的视图中时,它可以正常工作。

这是我的代码(地图视图是应用程序委托的一个属性,因为即使它不可见,我也需要用它做一些其他事情):

- (void)viewDidLoad
{
    ...
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showPan)];
    [appDelegate.mapView addGestureRecognizer:panGesture];
    [panGesture release];
}

- (void)showPan
{
    NSLog(@"pan!");
}

我使用最新的 iOS 4.2.1

感谢您的建议。

【问题讨论】:

    标签: objective-c cocoa-touch mkmapview uigesturerecognizer


    【解决方案1】:

    好吧,因为没人知道,我不得不为此花费了一次 Apple 技术支持咨询。 ;o)

    因为 MKMapView 显然有自己的识别器来与用户交互,所以你必须遵守 UIGestureRecognizerDelegate 协议并像这样实现(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

    - (void)viewDidLoad
    {
        ...
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showPan)];
        panGesture.delegate = self;
        [appDelegate.mapView addGestureRecognizer:panGesture];
        [panGesture release];
    }
    
    - (void)showPan
    {
        NSLog(@"pan!");
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {   
        return YES;
    }
    

    然后它就像一个魅力。

    【讨论】:

    • 天哪 - 谢谢,我需要一段时间才能弄清楚!
    • 重要的是,您仍然可以从 UIBuilder 定义手势识别器。所以从技术上讲,重要的是委托方法(如果您将其定义为 IBAction,您可以从构建器中按住 ctrl-拖动识别器到 showPan)
    • 你找到减速的解决方案了吗?当您处于滚动状态并释放时,地图会继续前进并减慢速度。我找不到捕捉它的方法。
    【解决方案2】:

    斯威夫特 5

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGesture))
    panGesture.delegate = self
    self.mapView.addGestureRecognizer(panGesture)
    
    @objc func panGesture (sender: UIPanGestureRecognizer) {
            
    }
        
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
       return true
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多