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