gpt4 book ai didi

ios - 在边界外接收触摸事件

转载 作者:IT王子 更新时间:2023-10-29 05:21:27 25 4
gpt4 key购买 nike

之前也有类似的问题,引用了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 作为它的 subview 。这个想法是让整个控件像下拉菜单一样。当按下 UIButton 时,UITableView 将下拉并启用选择。

如果控件是最顶层 UIView 的直接子项,这一切都适用于 UIButton 中覆盖的 hitTest,如上面 Apple 的问答链接中所述。但是,如果控件位于另一个 View 层次结构中,则 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)
}

编辑:

对于无法查看答案并检查他们是否解决了问题,我深表歉意,因为有一些其他任务需要立即关注。我将检查它们并接受一个有帮助的。感谢您的耐心等待。

最佳答案

正如您所说:

However, if the control is in another view hierarchy, the UITableView is not receiving touch events.

即使你让它工作了,如果你在另一个 UIView 下子查看了这个控件怎么办?

这样我们就有了为 View 层次结构转换点的负担,我的建议是,正如您在 this control 中看到的那样,它将 TableView 添加到控件本身所在的同一层次结构中。 (例如,它在添加此控件的 Controller 上添加 TableView )

链接的一部分:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setupSuggestionList];
[suggestionListView setHidden:NO];

// Add list to the super view.
if(self.dataSourceDelegate && [self.dataSourceDelegate isKindOfClass:UIViewController.class])
{
[((UIViewController *)self.dataSourceDelegate).view addSubview:suggestionListView];
}

// Setup list as per the given direction
[self adjustListFrameForDirection:dropDownDirection];
}

希望对您有所帮助!

关于ios - 在边界外接收触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com