UITableView 和 UIView 手势冲突
Posted
技术标签:
【中文标题】UITableView 和 UIView 手势冲突【英文标题】:UITableView and UIView gesture conflict 【发布时间】:2015-03-24 13:27:11 【问题描述】:我正在尝试在UIView
中添加UITableView
,但我遇到了手势识别器问题。这是我的代码:
_subView = [[UIView alloc] initWithFrame:CGRectMake(0, screenRect.size.height-100, screenRect.size.width, 300)];
_subView.backgroundColor = [UIColor colorWithRed:0.110f green:0.192f blue:0.298f alpha:1.00f];
[self.view addSubview:_subView];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(subViewToggle)];
[_subView addGestureRecognizer:singleFingerTap];
_moreTable = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenRect.size.width, 240)];
//Transforme to HOR scroll ;
CGRect frame = _moreTable.frame;
_moreTable.transform = CGAffineTransformRotate(CGAffineTransformIdentity, k90DegreesCounterClockwiseAngle);
_moreTable.frame = frame;
[_moreTable registerNib:[UINib nibWithNibName:@"MoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"moreCell"];
_moreTable.backgroundColor = [UIColor groupTableViewBackgroundColor];
_moreTable.delegate = self;
_moreTable.dataSource = self;
[_subView addSubview:_moreTable];
[_subView bringSubviewToFront:_moreTable];
问题是当我单击表格单元格而不是执行didSelectRowAtIndexPath
方法时,他执行subViewToggle
方法。我试图把我的tableView
放在前面,但没有帮助
【问题讨论】:
【参考方案1】:您的点击手势会吃掉触摸。有两种处理方式:
首先,您可以实现- gestureRecognizer:shouldReceiveTouch:
委托方法,并决定是否应该发生点击手势。
其次,你也可以将tap的cancelsTouchesInView
属性设置为NO,这样tableView就会接收到触摸。但点击也会在某个时候收到触摸。
【讨论】:
【参考方案2】:尝试实现 UIGestureRecognizerDelegate 协议的gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
这应该调用你的 didSelectRow 和 GestureRecognizer
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
【讨论】:
【参考方案3】:请尝试这样使用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
if ([touch.view == _moreTable])
return NO;
return YES;
【讨论】:
你的意思是if (touch.view == _moreTable)
这没有帮助我添加了委托,但它不起作用,因为 MyViewController 是 ScrollView 的子类并且不识别手势。【参考方案4】:
为了解决这个问题,我将 UIView 手势从单指点击更改为 moveUP 和 moveDown 手势。
请注意,我只尝试了 @dharmbir 解决方案,但它对我没有用。更新此外,实施 shouldReciveTouches
的这个解决方案适用于 tapGesture 和她的代码 sn-p :
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
// UITableViewCellContentView => UITableViewCell
if([touch.view.superview isKindOfClass:[UITableViewCell class]])
return NO;
// UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]])
return NO;
return YES;
【讨论】:
【参考方案5】://Create tap gesture for menu transparent view
UITapGestureRecognizer *rightTableTransparentViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTableTransparentViewTapMethod:)];
[rightTableTransparentViewTap setCancelsTouchesInView:NO];
[_rightTableTransparentView addGestureRecognizer:rightTableTransparentViewTap];
- (void)rightTableTransparentViewTapMethod:(UITapGestureRecognizer *)recognizer
//Write your code here
【讨论】:
以上是关于UITableView 和 UIView 手势冲突的主要内容,如果未能解决你的问题,请参考以下文章
UIView加入手势 然后UITableView 加入进这个View 导致UITableView 的单元格点击事件无效