UIScrollview 和其view上的事件冲突问题

来源:互联网 发布:自动打电话骚扰软件 编辑:程序博客网 时间:2024/06/05 18:07

问题现象:在scrollview的子类上添加  UITextview,uiwebview与scrollview父视图的点击事件冲突

第一步:

(当view上的单击事件与view上的子视图事件冲突时使用)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    // 输出点击的view的类名

    NSLog(@"%@",NSStringFromClass([touch.viewclass]));

    

    // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件

    if ([NSStringFromClass([touch.viewclass]) isEqualToString:@"UITableViewCellContentView"]) {

        return NO;

    }

    

    if ([NSStringFromClass([touch.viewclass]) isEqualToString:@"NDTPackageDetailInfoView"])

    {

        NSLog(@"shouldReceiveTouch NDTPackageDetailInfoView");

        return NO;

    }

    

    if ([NSStringFromClass([touch.viewclass]) 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 thetouchesShouldBegin: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属性设置为NOtextviewtouch事件从事件队列中移除,所以响应的是它的父视图

        //测试结果:

        returnYES;

    }

    if ([view isKindOfClass:[UIWebView class]])

    {

     // uiwebview中有阻止事件出现的方法

        returnNO;

    }

    returnNO;

}

0 0