collection添加手势后item不响应点击事件

1,856 阅读1分钟

效果图

效果图

textField和textView需要自己控制键盘的收回,而且要考虑到用户体验,所以此时要给collectionView添加手势,当添加手势之后你发现didSelectItem方法不被执行了

原因:didSelectItem方法被tap事件拦截了

解决方案1.

通过tap属性获取点击位置 判断point是否在_collectionCell上 在就执行didSelectItem方法(手动调用)

- (void)tap:(UITapGestureRecognizer *)tap{
    CGPoint point = [tap locationInView:_collection];
    NSIndexPath *index = [_collection indexPathForItemAtPoint:point];
    if (index != nil) {
        [self collectionView:_collection didSelectItemAtIndexPath:index];
    }
    [titleT resignFirstResponder];
    [contentTV resignFirstResponder];
}
解决方案2.

通过代理方法判断_collection是不是第一响应者(事件传递由上至下) 如果不是就拦截tap手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != _collection) {
        [_titleT resignFirstResponder];
        [contentTV resignFirstResponder];
        return NO;
    }
    return YES;
}

//解决方案3 //响应链方式(一个思路,还没实现)