MKMapView 上的 UIPanGestureRecognizer?

发布于 2024-10-17 15:23:55 字数 511 浏览 1 评论 0原文

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

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

- (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

感谢您的任何建议。

I would like to add some logic when user moves with map view i. e. he does a pan touch. But when I add the gesture recognizer and I want to log the touch, nothing happens. When I try it in another view controller and add the recognizer to controller's view then it works ok.

Here's my code (map view is a property of application delegate because I need to do some other things with it even if it isn't visible):

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

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

I use latest iOS 4.2.1

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-10-24 15:23:55

好吧,因为没有人知道,我不得不为此花费了一位苹果技术支持咨询费用。 ;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;
}

然后它就像一个魅力。

Ok, because no one knew, I had to spent one Apple technical support consult for it. ;o)

Because MKMapView evidently has its own recognizers to interact with user, you have to adhere to the UIGestureRecognizerDelegate protocol and implement (BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: like this:

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

Then it works like a charm.

夜清冷一曲。 2024-10-24 15:23:55

雨燕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
}

Swift 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
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文