我的UIWebView会响应来自用户的触摸事件,并且还有部分重叠UIWebView的UIView。我在重叠视图中添加了一个Pan手势识别器。当用户触摸重叠视图时,它会阻止webview触摸事件。当Overlapped视图被点击时,Webview是否有任何可能的方式接收触摸事件?感谢
答案 0 :(得分:3)
如果你只想点击重叠区域,叠加视图可以继承UIView并实现hitTest:withEvent:,下面将使重叠区域透明。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self && CGRectContainsPoint(overlappedArea, point)) {
return nil;
}
return hitView;
}
但是这将取消覆盖视图重叠区域中的所有触摸,从而使平移手势失败。另一种方法是将平移手势和轻击手势添加到其拥有的超级视图,在叠加视图区域中,启用平移手势,在基础视图的区域中,启用轻击手势。像这样:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
if (CGRectContainsPoint(self.overlayView.frame, touchPoint)) {
return YES;
} else {
return NO;
}
} else if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
if (CGRectContainsPoint(self.underlyingView.frame, touchPoint)) {
return YES;
} else {
return NO;
}
}
return YES;
}