我在视图上添加了两个手势。一个是轻击手势,另一个是轻扫手势。每当用户尝试滑动视图时,点击手势方法就会被调用(检测到轻击手势)。可能是什么原因 ?在这种情况下,点击手势应该被忽略。
下面是代码。
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemSwiped:)];
swipeGest.direction = UISwipeGestureRecognizerDirectionUp ;
[messageView addGestureRecognizer:swipeGest];
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemSelected:)];
[messageView addGestureRecognizer:gest];
答案 0 :(得分:0)
因为您在同一视图上使用多个手势。这可能会过度隐藏另一个属性。对于Ex:第一个手势识别器识别手势(在这种情况下点击)它取消所有触摸事件。因此,剩余的手势识别器永远不会完成识别,并且永远不会发射它们的事件。
要在同一视图中添加多个UIGestureRecognizer,您需要设置委托属性(UIGestureRecognizerDelegate
)
yourGesture.delegate = self
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (gestureRecognizer isKindOfClass: [UISwipeGestureRecognizer class] || gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]) {
return YES
} else {
return NO
}
}
答案 1 :(得分:0)
使用此委托方法来避免其他手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return false;
}else if([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]){
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return false;
}
return true;
}
答案 2 :(得分:0)
试试这个;
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemSwiped:)];
swipeGest.direction = UISwipeGestureRecognizerDirectionUp ;
[messageView addGestureRecognizer:swipeGest];
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemSelected:)];
gest.numberOfTapsRequired = 1;
[messageView addGestureRecognizer:gest];
-(void)itemSwiped:(UISwipeGestureRecognizer*)gesture
{
NSLog(@"swipe");
}
-(void)itemSelected:(UITapGestureRecognizer*)gesture
{
NSLog(@"tap");
}
答案 3 :(得分:0)
Change single tap to doubletap gesture. so you can be sure user only double tap or swipe. It is good advice from me please follow it.