ios - 如何使触摸事件通过 UIView(类似于指针事件 :none in CSS)?

标签 ios objective-c uiview uikit

在 CSS 中,pointer-events:none; 允许点击事件通过一个元素。我很好奇我是否可以在 iOS 上的 Objective-C 中为 UIView 做任何类似的事情。

Here's a jsfiddle to an example of pointer-events: none.

我能以某种方式为一个 UIView 通过覆盖 hitTest:withEvent: 实现相同的行为吗? 或者也许还有另一种方法可以做到这一点?

感谢您的帮助。

最佳答案

这是一个符合您最初直觉的便捷实现:

#import "TouchTransparentView.h"

@implementation TouchTransparentView

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return nil;
    } else {
        return hitView;
    }
}

@end

Swift 版本,@IB 升级

import UIKit

@IBDesignable
class PassthroughTouchView: UIView {

    @IBInspectable var passthroughTouchEvents : Bool = false

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if hitView == self && passthroughTouchEvents == true {
            return nil;
        } else {
            return hitView;
        }
    }
}

set view class in IB to ours toggle now accessible in IB

关于ios - 如何使触摸事件通过 UIView(类似于指针事件 :none in CSS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088158/

相关文章:

ios - iOS 中的 Google Plus 登录错误

ios - 由于存储在文档目录中的大量图像,应用程序正在接收内存警告。应用程序崩溃了

iOS:动画 View 移回原始位置

ios - 更改 NSLayoutConstraint 的常量

iphone - iOS:UIView 相对于 UIWindow 的起源

iOS CustomTableViewCell : elements not aligining correctly

ios - 值没有出现在我的其余代码中

ios - 文本字段的平均值取决于文本字段的数量 "used"

ios - 向后滑动时 UINavigationController 白线故障

ios - 在 iOS 上,可以在不在 drawRect 中完成的情况下绘制某些东西吗?