不确定我是否正确理解不是直系子女是什么意思。但是,一旦您到达子视图滚动的末尾,iOS 应该会自动为您执行此操作,它应该让您滚动父滚动。
为此,您需要为孩子实现:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate: (BOOL)decelerate{
scrollView.bounces = NO;
}
scrollView.bounces = NO 不允许 scrollView 在结束滚动时跳回。所以它只是停留在停止滚动的位置。
然后您需要从子级的scrollViewDidScroll 方法触发父级滚动。
类似于:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scrollViewHeight = scrollView.frame.size.height;
CGFloat scrollContentSizeHeight = scrollView.contentSize.height;
CGFloat scrollOffset = scrollView.contentOffset.y;
CGFloat offset = scrollContentSizeHeight - (scrollOffset + scrollViewHeight);
if (offset <= 0)
[self.parentView setContentOffset:CGPointMake(self.parentView.contentOffset.x, self.parentView.contentOffset.y + diffOffset) animated:YES];
}
其中diffOffset 是您希望 parentScroll 滚动的距离。
您可能更喜欢更高级的解决方法。
例如你可以实现
scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
所以您知道滚动的velocity 是什么,比较targetContentOffset 与scrollContentSizeHeight 和scrollViewHeight 应该让您知道这个特定的滚动即将结束拖动到达滚动视图内容的末尾。
你可以更准确地计算diffOffset和animationTime。
当子视图在父滚动中不完全可见时阻止滚动可能会很好,因此滚动功能仅在您向上滚动到某个位置时才会出现。