一:
在自定义UITableViewCell 和UICollectionViewCell的时候,需要有个scrollerView滑动的时候,发现 didSelectItemAtIndexPath: indexPath 这个点击方法被覆盖了,不执行。
解决办法:
在自定义Cell中:
self.scrollView.userInteractionEnabled = NO;
[self.contentView addGestureRecognizer: self.scrollView.panGestureRecognizer];
这样子就可以完美解决Cell的点击事件被覆盖无法执行的问题了
但是这又导致了另外一个问题的出现,scrollview上的按钮不能接受点击事件
解决方案:
重写scrollview的
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法
点击按钮区域 返回按钮
点击非按钮区域 返回 return [super hitTest:point withEvent:event];
二:
响应者链条是从点击的当前控件中自上而下的,如果当前控件不能处理点击事件的话再向下传递,如果处理点击事件的话会执行三个方法(还有一个就先不说了):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
但是经过测试发现scrollView中只有点击事件才会执行此三种方法,而滑动事件则不会。而我们正是要的这种效果,如果滑动的话界面是不跳转的,点击才跳转,在这里我们在scrollView中这样操作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
albumTableViewCell * cell = self.superview;
[cell touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
albumTableViewCell * cell = self.superview;
[cell touchesEnded:touches withEvent:event];
}
其实只写touchesend那个方法就可以,将点击事件传递下去,在执行了touchEnd之后tableView的代理方法didSelect才会执行
,这样实现了点击事件的手动传递,效果还是不错的~~!
注意:
这种重写touch方法的idea,在cell套table再套scroll时会偶发性的出现获取点击的cell不对的问题
我暂时采用的是hittest的方式解决的
顺便说一下关闭控件响应事件的三种方法,
一个是属性userInteractionEnabled设成No、hidden属性设成yes、alpha在0~0.01之间,
其实还有一种,就是重写控件将hitTest:withEvent:方法,将返回置为空(因为响应者链条是这个方法传递的)。