UIView中添加子视图UISCrollview后UITouch事件不响应

来源:互联网 发布:用友t3重装数据库 编辑:程序博客网 时间:2024/05/17 01:46

做跑马灯组件时,需要在UIView视图中添加子视图UIScrollview,以便可以进行多个内容的向上滚动显示,同时也在UIView中添加了UITouch事件,但是UITouch点击却没有响应。

怎么解决呢?

出现问题原因:UIScrollView自身就有手势响应的事件,从响应链原理来看,触发响应后,手势事件被UISCrollView拦截了,不会再往下传递给UIScrollView的父视图,即UIView,所以不会响应UIView中的UITouch事件。

解决方案:

创建一个UIScrollView的category,重写UITouch方法。

如:UIScrollView+UITouch.h、UIScrollView+UITouch.m,并在UIScrollView+UITouch.m文件中重写UITouch方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [[self nextResponder] touchesBegan:touches withEvent:event];    [super touchesBegan:touches withEvent:event];}- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [[self nextResponder] touchesCancelled:touches withEvent:event];    [super touchesCancelled:touches withEvent:event];}- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [[self nextResponder] touchesEnded:touches withEvent:event];    [super touchesEnded:touches withEvent:event];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [[self nextResponder] touchesMoved:touches withEvent:event];    [super touchesMoved:touches withEvent:event];}

注意:响应链原理,事件从上往下传递

找到touch事件处理的当前视图,然后就由他接收,之后的响应处理(responder)从下往上传递(UIScrollView——>UIView——>UIViewController——>UIWindow——>Appdelegate)。





原创粉丝点击