iOS do not forward touches to views below

2019-09-10 10:31发布

问题:

In my app I have UIScrollView which has many UITableViews. In cells of this table there is UISlider. How I can disable touch forwarding if user make scroll gesture on table view? Now if user make scroll gesture near slider (but not on it) the touch event gets forwarder to scroll view.

回答1:

You can subclass UIScrollView and add this method to the new class

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView *result = [super hitTest:point withEvent:event] ;
    self.scrollEnabled = ![result isKindOfClass:[UISlider class]] ;
    return result ;

}