I allow users to tap anywhere on the screen and I also allow them to tap on annotations to display text related to the annotation.
我允许用户点击屏幕上的任何位置,我还允许他们点击注释以显示与注释相关的文本。
To allow them to tap anywhere on the screen I have setup a tapgesturerecoginzer.
为了让他们能够点击屏幕上的任何地方,我设置了一个tapgesturerecoginzer。
The problem is that when they tap on an annotation, the tapgesturerecognizer event happens before the didSelectAnnotationView, and so I perform the tapgesturerecognizer when I do not want to.
问题是,当他们点击注释时,tapgesturerecognizer事件发生在didSelectAnnotationView之前,因此我在不想要时执行tapgesturerecognizer。
Can I somehow test, in the tapgesturerecognizer, if they have tapped on an annotation ?
我可以在tapgesturerecognizer中以某种方式测试他们是否已经点击了注释?
2 个解决方案
#1
1
I have face the same issue with LongGesture
you can use UIGestureRecognizerDelegate
to prevent calling gesture action.
我遇到了与LongGesture相同的问题,你可以使用UIGestureRecognizerDelegate来阻止调用手势动作。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[MKPinAnnotationView class]])
{
return NO;
}
return YES;
}
Note: Don't forgot to set delegate with your tapGesture
.
注意:不要忘记使用tapGesture设置委托。
#2
0
Solution :
方案:
In my header (.h) I added the delegate:
在我的标题(.h)中,我添加了委托:
@interface myVC : UIViewController <UIGestureRecognizerDelegate>
In .m :
在.m:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.mapView addGestureRecognizer:tap];
tap.delegate = self;
Note the delegate statement that Nirav said needed to be added
请注意Nirav所说的代表声明需要添加
then I added Nirav's code but changed the IF statement :
然后我添加了Nirav的代码,但更改了IF语句:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (([touch.view isKindOfClass:[MKPinAnnotationView class]]) || ([touch.view isKindOfClass:[MKAnnotationView class]]))
{
return NO;
}
return YES;
}
#1
1
I have face the same issue with LongGesture
you can use UIGestureRecognizerDelegate
to prevent calling gesture action.
我遇到了与LongGesture相同的问题,你可以使用UIGestureRecognizerDelegate来阻止调用手势动作。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[MKPinAnnotationView class]])
{
return NO;
}
return YES;
}
Note: Don't forgot to set delegate with your tapGesture
.
注意:不要忘记使用tapGesture设置委托。
#2
0
Solution :
方案:
In my header (.h) I added the delegate:
在我的标题(.h)中,我添加了委托:
@interface myVC : UIViewController <UIGestureRecognizerDelegate>
In .m :
在.m:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.mapView addGestureRecognizer:tap];
tap.delegate = self;
Note the delegate statement that Nirav said needed to be added
请注意Nirav所说的代表声明需要添加
then I added Nirav's code but changed the IF statement :
然后我添加了Nirav的代码,但更改了IF语句:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (([touch.view isKindOfClass:[MKPinAnnotationView class]]) || ([touch.view isKindOfClass:[MKAnnotationView class]]))
{
return NO;
}
return YES;
}