gpt4 book ai didi

ios - 如何使触摸事件影响容器 View 背后的 View ?

转载 作者:可可西里 更新时间:2023-11-01 04:34:40 26 4
gpt4 key购买 nike

我有一个完全覆盖另一个 UIView 的容器 View 。容器 View 具有透明度以及其他一些东西(搜索栏、表格 View 等)。当事件发生在透明区域时,我希望触摸事件通过容器 View 并影响下面的 View 。

我一直在摆弄容器 View 的子类。我试图让 pointInside: 方法根据上述标准(透明容器 View )返回 YES 或 NO。据我所知,我的问题是我只能访问容器 View subview ,而不能访问完全位于容器 View 下方的 View 。

我目前一直在使用一种非常低效的方法来读取触摸像素的 alpha。执行此操作的最佳方法是什么?

最佳答案

如果你只是想让触摸通过你的容器 View ,同时仍然让它的 subview 能够处理触摸,你可以子类化 UIView 并覆盖 hitTest:withEvent: 就像这个:

swift :

class PassthroughView: UIView {

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
// Get the hit view we would normally get with a standard UIView
let hitView = super.hitTest(point, with: event)

// If the hit view was ourself (meaning no subview was touched),
// return nil instead. Otherwise, return hitView, which must be a subview.
return hitView == self ? nil : hitView
}
}

objective-C :

@interface PassthroughView : UIView
@end

@implementation PassthroughView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// Get the hit view we would normally get with a standard UIView
UIView *hitView = [super hitTest:point withEvent:event];

// If the hit view was ourself (meaning no subview was touched),
// return nil instead. Otherwise, return hitView, which must be a subview.
return hitView == self ? nil : hitView;
}

@end

然后让您的容器 View 成为该类的一个实例。此外,如果您希望触摸通过上述 View Controller 的 View ,则需要使该 View Controller 的 View 也是该类的一个实例。

关于ios - 如何使触摸事件影响容器 View 背后的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25238722/

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