gpt4 book ai didi

ios - 如何忽略触摸事件并将它们传递给另一个 subview 的 UIControl 对象?

转载 作者:IT王子 更新时间:2023-10-29 07:33:32 26 4
gpt4 key购买 nike

我有一个自定义的 UIViewController,它的 UIView 占据了屏幕的一角,但大部分是透明的,除了上面有一些按钮和东西的部分。由于该 View 上对象的布局, View 的框架可能会掩盖其下方的一些按钮。如果他们没有触摸任何重要的东西,我希望能够忽略对该 View 的任何触摸,但我似乎只能传递实际的触摸事件(touchesEnded/nextResponder 东西)。如果我有一个不使用 touchesEnded 的 UIButton 或类似的东西,我该如何将触摸事件传递给它?

我不能只是手动找出要调用的按钮选择器,因为这个自定义 ViewController 可以用于许多不同的 View 。我基本上需要一种方法来调用它:

[self.nextResponder touchesEnded:touches withEvent:event];

还有 UIControl 类型。

最佳答案

可能最好的方法是在您希望忽略触摸的 View 中覆盖 hitTest:withEvent:。根据您的 View 层次结构的复杂性,有几种简单的方法可以做到这一点。

如果您有对要忽略的 View 下方的 View 的引用:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];

// If the hitView is THIS view, return the view that you want to receive the touch instead:
if (hitView == self) {
return otherView;
}
// Else return the hitView (as it could be one of this view's buttons):
return hitView;
}

如果您没有对 View 的引用:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];

// If the hitView is THIS view, return nil and allow hitTest:withEvent: to
// continue traversing the hierarchy to find the underlying view.
if (hitView == self) {
return nil;
}
// Else return the hitView (as it could be one of this view's buttons):
return hitView;
}

我会推荐第一种方法,因为它是最可靠的(如果有可能获得对底层 View 的引用)。

关于ios - 如何忽略触摸事件并将它们传递给另一个 subview 的 UIControl 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719412/

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