【发布时间】:2016-10-16 21:45:09
【问题描述】:
之前也有类似的问题,我参考过Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:和Delivering touch events to a view outside the bounds of its parent view。但他们似乎没有回答我的特殊问题。
我有一个具有以下结构的自定义控件:
+-------------------------------+
| UIButton |
+-------------------------------+
| |
| |
| UITableView |
| |
| |
+------------------------- +
自定义控件覆盖UIButton 子类并将UITableView 添加为其子视图。这个想法是让整个控件像下拉菜单一样。当按下UIButton 时,UITableView 将下拉并启用选择。
如果控件是最上面的UIView 的直接子控件,则这一切都适用于UIButton 中覆盖的UIButton 中的hitTest,如上面Apple 的问答链接中所述。但是,如果控件位于另一个视图层次结构中,则UITableView 不会接收到触摸事件。
以下是使用的hitTest代码:
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
let pointForTargetView = self.spinnerTableView.convertPoint(point, fromView:self)
if (CGRectContainsPoint(self.spinnerTableView.bounds, pointForTargetView)) {
// The target view may have its view hierarchy,
// so call its hitTest method to return the right hit-test view
return self.spinnerTableView.hitTest(pointForTargetView, withEvent:event);
}
return super.hitTest(point, withEvent: event)
}
编辑:
很抱歉,由于需要立即关注其他一些任务,因此无法查看答案并检查它们是否解决了问题。我会检查它们并接受一个有帮助的。感谢您的耐心等待。
【问题讨论】: