问题现象:在scrollview的子类上添加 UITextview,uiwebview与scrollview父视图的点击事件冲突
第一步:
(当view上的单击事件与view上的子视图事件冲突时使用)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 输出点击的view的类名
NSLog(@"%@", NSStringFromClass([touch.view class]));
// 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
if ([NSStringFromClass([touch.view class]) isEqualToString:@"NDTPackageDetailInfoView"])
{
NSLog(@"shouldReceiveTouch NDTPackageDetailInfoView");
return NO;
}
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIWebView"]) {
return NO;
}
if (self.isProductDetailShow)
{
return NO;
}
return YES;
}
第二步:摘自--官方文档
Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.
如果其上放置一个UITextview
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
if ([view isKindOfClass:[UITextView class]])
{
//如果:textview接收,没有任何响应,传给根视图
//如果:userInteractionEnabled属性设置为NO;textview的touch事件从事件队列中移除,所以响应的是它的父视图
//测试结果:
return YES;
}
if ([view isKindOfClass:[UIWebView class]])
{
// uiwebview中有阻止事件出现的方法
return NO;
}
return NO;
}