iOS UIScrollView嵌套UIScrollView(U
问题描述:
UIScrollView嵌套UIScrollView(UITableView)时,当子视图滑动到边界时,滑动手势无法传递给父视图。
解决方案:
重写子视图的gestureRecognizerShouldBegin方法,当子视图滑动到边界时,将pan手势传递到父视图。
代码:
// 判断scrollView是不是在最顶部往下滑或者在最底部往上滑,如果是这两种情况才需要把事件往下传递
- (BOOL)isScrollViewOnTopOrBottom {
CGPoint translation = [self.panGestureRecognizer translationInView:self];
if (translation.y > 0 && self.contentOffset.y <= 0) {
return YES;
}
CGFloat maxOffsetY = floor(self.contentSize.height - self.bounds.size.height);
if (translation.y < 0 && self.contentOffset.y >= maxOffsetY) {
return YES;
}
return NO;
}
#pragma mark - GestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.panGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerStatePossible) {
if ([self isScrollViewOnTopOrBottom]) {
return NO;
}
}
}
return YES;
}
参考文章:
http://skx926.com/2017/05/28/nested-scrollview-scrolling-conflit/