UITableView和UIView手势冲突

时间:2015-03-24 13:27:12

标签: ios objective-c uitableview uiview uigesturerecognizer

我正在尝试在UITableView内添加UIView,但我的手势识别器出现问题。这是我的代码:

    _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带到前面,但没有帮助

5 个答案:

答案 0 :(得分:1)

你的轻拍手势吃了一下。有两种方法可以处理:

首先,您可以实施- gestureRecognizer:shouldReceiveTouch:委托方法,并决定是否应该使用点击手势。

第二,你也可以将tap的cancelsTouchesInView属性设置为NO,这样tableView就会收到触摸。但是也会在某个时候点击一下。

答案 1 :(得分:0)

尝试实施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

答案 2 :(得分:0)

请尝试像这样使用

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view == _moreTable]) {

        return NO;
    }

    return YES;
}

答案 3 :(得分:0)

为了解决这个问题,我已经将UIView手势从单指点击改为moveUP和moveDown手势。 请注意,我只尝试过@dharmbir解决方案,但它并没有为我工作。

更新此实施shouldReciveTouches的解决方案适用于tapGesture及其代码段:

 -(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;
}

答案 4 :(得分:0)

//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
}