我刚刚面临这个问题。根据@Guntis Treulands 的建议,我决定检查如果我hitTest:withEvent:
在自定义标题视图中覆盖方法会发生什么。此方法忽略隐藏、禁用用户交互或 alpha 级别小于 0.01 的视图对象。此方法在确定命中时不考虑视图的内容。因此,即使指定的点在视图内容的透明部分中,仍然可以返回视图,现在,在它被覆盖之后,接收到边界之外的触摸。它对我有用。希望它可以帮助你,伙计们。
迅速
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard !isHidden, alpha > 0 else {
return super.hitTest(point, with: event)
}
for subview in subviews {
let subpoint = subview.convert(point, from: self)
let result = subview.hitTest(subpoint, with: event)
if result != nil {
return result
}
}
return super.hitTest(point, with: event)
}
Objective-C
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subPoint withEvent:event];
if (result != nil) {
return result;
}
}
}
// use this to pass the 'touch' onward in case no subviews trigger the touch
return [super hitTest:point withEvent:event];
}