[英]How do override hitTest to allow my subview to receive touch events?
我有一个UIView包含另一个视图,它本身包含一个UIButton。 问题是MENUVIEW确实很窄,所以我的DROPDOWN扩展到它上面,当它打开时,用户轻按(K)不会发生任何事情。 我知道我必须重写HitTest:和pointInside:但我不确定在哪个视图中以及如何使用?
是否在A,B或K中覆盖hitTest? 然后指向A,B或K里面?
例:
(A) is MENUVIEW, the parent of all views
(B) is DROPDOWN, a subview of MENUVIEW
(K) is UIBUTTON, a subview of DROPDOWN
图解:
+---------------+
|A |
| |
| |
| |
|+--------------|----------+
||B | +K---+ |
|| | +----+ |
|+--------------|----------+
+---------------+
更新:
这是我放入的代码,但是不起作用。 在下面的代码中,我单击B,然后B将打开并显示K,但是K仍然不可单击。 里面的点不在(A)上吗?
#pragma mark - Clickable Area()
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat radius = 200.0;
CGRect frame = CGRectMake(self.dropDownMenu.frame.origin.x, self.dropDownMenu.frame.origin.y + 50,
200,
174); <-- this B's frame area minus the part of B inside A (i.e. just K and the part of B outside A)
if (CGRectContainsPoint(frame, point)) {
return self.dropDownMenu;
} else {
return [super hitTest:point withEvent:event];
}
return nil;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.bounds, point)) {
return YES;
}
return NO;
}
@end
你应该做一个足够大的parentview。 并在点不在a | b | c区域中时覆盖d的pointInside和hittest,返回nil以使下面的视图响应事件。(返回nil表示userinteractionenabled = NO)
- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
1.convert b's frame to the view D
if (point inside B's convertedFrame) {
return B;
}
xxxxx {
return A
}
XXXXX {
return C;
}
return nil;
}
+-----------------------------+
+---------------+ D |
|A | |
| | |
| | |
| | |
|+--------------|----------+ |
||B | +K---+ | |
|| | +----+ | |
|+--------------|----------+ |
+---------------+ |
+-----------------------------+
如果b响应事件,那么k甚至可以响应a内的k。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.