触摸事件由多个视图处理
我有一个UITableView
的UIView
的子类。 我正在使用UITableView
来显示一些数据,同时,我想覆盖手指后面的animation(例如,留下踪迹)。
如果我得到它的权利,我需要触摸事件由UIView
子类和UITableView
。 我怎样才能做到这一点? 是否有可能,即touchesMoved
在UIView子类,然后在UITableView
触发?
非常感谢你的帮助。
我已经解决了这个问题的方式是不那么干净,但它的工作原理。 请让我知道是否有更好的方法来做到这一点。
我已经重写了我的自定义UIView
hitTest
,所以它指向下面的UITableView
触摸。 然后在UITableView
我通过touchesBegan
, touchesMoved
等处理手势。在那里,我也调用UIView
上的touchesBegan
。
这样触摸就是由两个视图来处理的。 我之所以不这样做(有UIView
的touchesBegan
调用UITableView
的touchesBegan
)是因为UITableView
上的手势识别器不起作用。
UIView
子类“ hitTest
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { // tview is the UITableView subclass instance CGPoint tViewHit = [tView convertPoint:point fromView:self]; if ([tView pointInside:tViewHit withEvent:event]) return tView; return [super hitTest:point withEvent:event]; }
UITableView
子类的touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:touch.view]; // .... // view is the UIView's subclass instance [view touchesBegan:touches withEvent:event]; }
不,你不能这么做。 事件传递章节说
窗口对象使用命中testing和响应者链来查找接收触摸事件的视图。 在命中testing中,窗口调用hitTest:withEvent:在视图层次结构的最顶层视图上; 此方法通过recursion调用pointInside:withEvent:在视图层次结构中的每个视图中返回YES,继续向下继续,直到find触摸发生的边界内的子视图。 这个观点成为了命中testing的观点。
所以,当窗口发现触摸视图时,它返回YES。 只有一个视图可以在当前处理触摸。
但是,如果你需要处理UITableView的事件,然后处理它的UIView! 您可以将触摸的点转换为所需的坐标与- convertPoint , – convertRect函数,将子视图添加到UITableView并移动它取决于坐标,还有很多其他的东西。
UITableView将未处理的触摸事件传递给UIView。 (Google“响应者链”) UITableView文档
所以,你只能在UIView中处理你的触摸事件。 所以。 在你的UIView
-
touchesstart – 做初始化的东西
-
touchesmove – 在UIView上绘制尾巴(使用定时器/延迟响应到desable点,使它看起来像一个踪迹)
-
touchesend – 做剩下的东西
希望这可以帮助。
- 在没有debugging器的情况下,标准输出redirect在iOS中不起作用
- 如何definitiveley设置UITabBar背景颜色和UITabBar色彩
- 如何在UIImageView.animationImages之后清除内存?
- 删除核心数据关系规则
- WhatsApp如何在iOS上encryption推送通知?
- 在Swift中使用Reachability,NSNotification和Network Link Conditioner检测networking连接变化
- 在iOS中,使用CGPDFDocumentRef,为什么我的PDF显示很大的利润,而不是整个页面?
- 为UIWebView使用UIRefreshControl
- AFnetworking waitUntilFinish不起作用