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

我不能仅仅手动确定要调用的按钮 Select 器,因为这个自定义的ViewController可以在许多不同的视图上使用.我基本上需要一种方式来称呼它:

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

在UIControl类型上也是如此.

推荐答案

最好的方法可能是在您希望忽略接触的视图中覆盖hitTest:withEvent:.根据视图层次 struct 的复杂程度,有几种简单的方法可以做到这一点.

如果要忽略视图下方的视图引用,请执行以下操作:

- (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;
}

如果您没有对视图的引用:

- (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;
}

我建议第一种方法是最健壮的(如果有可能获得对底层视图的引用).

Ios相关问答推荐

SWIFT中具有可选返回类型和泛型的网络请求

如何在SwiftUI ScrollView中zoom 中心项目?

如何在Flutter 中为S家长增加一个相对高度的间距

在iOS 17脉冲符号效果不起作用的情况下制作UIBarButtonItem动画(没有UIImageView)

TrueDepth摄像机像素距离不准确

仅更改 AttributedString 的字符串(例如 UIButton 配置 attributeTitle)

保存和读取登录到 keys 串不工作 IOS swift

比较 `某个协议` 实例时出现编译错误

SwiftUI 菜单中的内联水平按钮

FileHandle 不会在 iOS 中释放内存

'DismissAction'类型的值没有成员'wrappedValue'在SwiftUI的Xcode中

如何包含 Xcode 子项目标头?

Xcode 13.4.1 (13F100)Command CodeSign 失败,退出代码非零我应该删除哪个证书?

SwiftUI - 使用切换switch 切换键盘类型(默认/数字)?

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

setNeedsLayout 和 setNeedsDisplay

如何更新推送通知服务证书

iTunes Connect 中的无限制 Web 访问是什么意思

我的应用程序因使用广告支持框架而被拒绝.哪个图书馆负责?

在自动布局中居中子视图的 X 会引发未准备好约束