简体   繁体   English

在UIView和UITableView中冲突的UITapGestureRecognizer

[英]Conflicting UITapGestureRecognizer in UIView and for UITableView

I have a UIView in which I added a UITapGestureRecognizer . 我有一个UIView ,其中我添加了一个UITapGestureRecognizer Inside that view I also have a subview in which is basically some kind of a UITableView . 在该视图中我还有一个子视图,其中基本上是某种UITableView The question is that why doesn't the UITableView recognizes the tap on a row, instead it goes to the tap gesture recognizer's handler all the time. 问题是为什么UITableView不能识别行上的点击,而是一直进入点击手势识别器的处理程序。 Why is this, and how do I solve this? 这是为什么,我该如何解决这个问题? If I set the number of taps to 2, then it works fine. 如果我将点击数设置为2,那么它可以正常工作。 Any idea on how to solve this? 关于如何解决这个问题的任何想法? Basically it doesn't call the didSelectRowAtIndexPath . 基本上它不会调用didSelectRowAtIndexPath

Set cancelsTouchesInView of your recognizer to NO . 将识别器的cancelsTouchesInView设置为NO Otherwise, it "consumes" the touch for itself, and does not pass it on to the table view. 否则,它会“消耗”触摸本身,并且不会将其传递给表格视图。 That's why the selection event never happens. 这就是选择事件永远不会发生的原因。

If you want both your UITableView and your UITapGestureRecognizer to receive touch events, then yes the cancelsTouchesInView = NO will work. 如果你希望你的这两个 UITableView UITapGestureRecognizer接收触摸事件,然后是在cancelsTouchesInView = NO会工作。 If you want the tap gesture recognizer not to receive the touch events meant for the table view it is slightly less easy but very do-able. 如果您希望轻敲手势识别器不接收针对表格视图的触摸事件,则稍微容易但非常容易。

Basically when you are creating your gesture recognizer you set self as its delegate. 基本上,当您创建手势识别器时,您将self设置为其委托。 Then you implement the gestureRecognizer:shouldReceiveTouch: delegate method. 然后实现gestureRecognizer:shouldReceiveTouch: delegate方法。 A basic implementation might look like this. 基本实现可能如下所示。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    UITableView *tableView = self.tableView;
    CGPoint touchPoint = [touch locationInView:tableView];
    return ![tableView hitTest:touchPoint withEvent:nil];
}

Essentially this method (as implemented) asks the tableView if this touch's location falls within the tableView 's jurisdiction, and if it does, it will block the gesture recognizer from receiving the touch...allowing the tableView to receive the touch. 基本上这个方法(如实现的那样)询问tableView这个触摸的位置是否属于tableView的管辖范围,如果是,它将阻止手势识别器接收触摸......允许tableView接收触摸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM