我想在视图中一起使用轻击手势和长按手势。但我的问题是,我无法在点击时运行轻击手势动作。但长按手势工作正常。
以下是代码段。
UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
longPressGesture.minimumPressDuration=0.6;
longPressGesture.delegate=self;
[cell.view addGestureRecognizer:longPressGesture];
UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellSelected:)];
//[gesture requireGestureRecognizerToFail:longPressGesture]; //I have tried with this line also but not working
gesture.delegate=self;
[cell.view addGestureRecognizer:gesture];
我也设置了委托方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
长按
调用此方法- (void)ontappLongPressGesture:(id)sender{
//Long press code here
}
但这种方法没有被点击
-(void)cellSelected:(id)sender {
//Single tap code here
}
答案 0 :(得分:4)
您没有指定将这些gestureRecognizer放在哪种类型的视图上,但是因为您将其称为“单元格”,我假设它在UITableView上?
如果是这样,您需要确保设置cancelsTouchesInView
标志:
gesture.cancelsTouchesInView=NO;
答案 1 :(得分:2)
您需要使用以下两种方式之一。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if (cell.view.superview != nil) {
if ([touch.view isDescendantOfView:cell.view]) {
// we touched our control surface
return YES; // handle the touch
}
}
return NO; // ignore the touch
}
在这里,您需要指定您想要gestureRecognizer的视图。
或者你也可以使用这些代码行
gesture.cancelsTouchesInView = NO;
longPressGesture.cancelsTouchesInView = NO;
希望它会对你有所帮助。
答案 2 :(得分:1)
据我了解你使用UITableViewCell的那些手势。你为什么不用tableView:didSelectRowAtIndexPath:
?