iPhone:将 MKMapView 与另一个 UITapGestureRecognizer 结合起来

发布于 2024-11-29 01:29:37 字数 1127 浏览 1 评论 0原文

除了 MKMapView 已经使用的手势识别器之外,我还尝试实现自己的手势识别器。现在我可以点击地图并设置图钉。这种行为是由我的 UITapGestureRecognizer 实现的。当我点击已经存在的引脚时,我的手势识别器不执行任何操作,而是显示该引脚的标注气泡。 UIGestureRecognizerDelegate 看起来像这样:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (gestureRecognizer == self.tapRecognizer) 
    {
        bool hitAnnotation = false;
        int count = [self.mapView.annotations count];
        int counter = 0;

        while (counter < count && hitAnnotation == false ) 
        {
            if (touch.view == [self.mapView viewForAnnotation:[self.mapView.annotations objectAtIndex:counter]]) 
            {
                hitAnnotation = true;
            }
            counter++;
        }
        if (hitAnnotation) 
        {
            return NO;
        }

    }
    return YES; 
}

这工作正常。我唯一的问题是引脚和双击的标注气泡。通常双击用于放大。这仍然有效,但除此之外,我还获得了一个新的图钉。有什么办法可以避免这种情况吗?

另一个问题发生在图钉的标注气泡上。我可以通过点击图钉来打开气泡,而无需在此位置设置新图钉(请参见上面的代码),但是当我想通过点击图钉来关闭气泡时,会设置另一个图钉。我的问题是,如果用户点击标注气泡,我无法检查 touch.view ,因为据我所知,它不是常规的 UIView 。对于这个问题有什么想法或解决方法吗?

谢谢

i am trying to implement my own gesture recognizer in addition to the one already used by the MKMapView. Right now i can tap on the map and set a pin. This behavior is realized by my UITapGestureRecognizer. When i tap on a pin that already exists, my gesture recognizer does nothing, but instead the callout bubble of this pin is shown. The UIGestureRecognizerDelegate looks like this:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (gestureRecognizer == self.tapRecognizer) 
    {
        bool hitAnnotation = false;
        int count = [self.mapView.annotations count];
        int counter = 0;

        while (counter < count && hitAnnotation == false ) 
        {
            if (touch.view == [self.mapView viewForAnnotation:[self.mapView.annotations objectAtIndex:counter]]) 
            {
                hitAnnotation = true;
            }
            counter++;
        }
        if (hitAnnotation) 
        {
            return NO;
        }

    }
    return YES; 
}

This works fine. My only problem are the callout bubbles of the pins and the double tap. Normally the double tap is used for zooming in. This still works but in addition to this, i also get a new pin. Is there any way to avoid this?

The other problem occurs with the callout bubble of a pin. I can open the bubble by tapping on the pin without setting a new pin at this place (see code above) but when i want to close the bubble by tapping on it, another pin is set. My problem is, that i cannot check with touch.view , if the user tapped on a callout bubble, because it is not a regular UIView as far as i know. Any ideas or workarounds for this problem?

Thanks

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

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

发布评论

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

评论(1

德意的啸 2024-12-06 01:29:37

我遇到了与您的第一个问题相同的问题:区分 MKMapView 中的双击和单击。我所做的如下:

[doubleTapper release];
doubleTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapDoubleTapped:)];
doubleTapper.numberOfTapsRequired = 2;
doubleTapper.delaysTouchesBegan = NO;
doubleTapper.delaysTouchesEnded = NO;
doubleTapper.cancelsTouchesInView = NO;
doubleTapper.delegate = self;

[mapTapper release];
mapTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)];
mapTapper.numberOfTapsRequired = 1;
mapTapper.delaysTouchesBegan = NO;
mapTapper.delaysTouchesEnded = NO;
mapTapper.cancelsTouchesInView = NO;
[mapTapper requireGestureRecognizerToFail:doubleTapper];

然后实现以下委托方法:

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

使用 requireGestureRecognizerToFail: 允许应用程序区分单击和双击,并实现 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 确保双击仍会转发到 MKMapView,以便它继续正常缩放。请注意,doubleTapper 实际上并没有执行任何操作(在我的例子中,除了记录调试消息)。它只是一个虚拟的 UIGestureRecognizer,用于帮助区分单击和双击。

I had the same problem as your first problem: distinguishing double taps from single taps in an MKMapView. What I did was the following:

[doubleTapper release];
doubleTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapDoubleTapped:)];
doubleTapper.numberOfTapsRequired = 2;
doubleTapper.delaysTouchesBegan = NO;
doubleTapper.delaysTouchesEnded = NO;
doubleTapper.cancelsTouchesInView = NO;
doubleTapper.delegate = self;

[mapTapper release];
mapTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)];
mapTapper.numberOfTapsRequired = 1;
mapTapper.delaysTouchesBegan = NO;
mapTapper.delaysTouchesEnded = NO;
mapTapper.cancelsTouchesInView = NO;
[mapTapper requireGestureRecognizerToFail:doubleTapper];

and then implemented the following delegate method:

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

Using requireGestureRecognizerToFail: allows the app to distinguish single taps from double taps and implementing gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: ensures that double taps are still forwarded to the MKMapView so that it continues zooming normally. Note that doubleTapper doesn't actually do anything (in my case, except log debug messages). It's simply a dummy UIGestureRecognizer that's used to help separate single taps from double taps.

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