iPhone:将 MKMapView 与另一个 UITapGestureRecognizer 结合起来
除了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了与您的第一个问题相同的问题:区分
MKMapView
中的双击和单击。我所做的如下:然后实现以下委托方法:
使用
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:and then implemented the following delegate method:
Using
requireGestureRecognizerToFail:
allows the app to distinguish single taps from double taps and implementinggestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
ensures that double taps are still forwarded to theMKMapView
so that it continues zooming normally. Note thatdoubleTapper
doesn't actually do anything (in my case, except log debug messages). It's simply a dummyUIGestureRecognizer
that's used to help separate single taps from double taps.