我还没有找到将事件传递给正确滚动视图的方法。但这里有一些让它起作用的东西:
首先,您需要在所有嵌套的 UIScrollView 中禁用 Paging Enabled(因为嵌套的 UIScrollView 将与父 UIScrollView 一起滚动)。
实现嵌套 ScrollViews 的分页:
/*
* User stopped dragging the innerScroll, the view is not decelerating
* and it is still not at its place. Lets help the view to get into right place.
*/
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if ([scrollView isEqual:innerScroll] && !decelerate) {
if(scrollView.contentOffset.x <= view1.frame.size.width/2){
[innerScroll scrollRectToVisible:view1.frame animated:YES];
} else {
[innerScroll scrollRectToVisible:view2.frame animated:YES];
}
}
}
/*
* User stopped dragging the innerScroll and the View is decelerating.
* Lets skip an efforts of the View to get into right place and put it ourselves.
*/
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
if ([scrollView isEqual:innerScroll]) {
if(scrollView.contentOffset.x <= view1.frame.size.width/2){
[innerScroll scrollRectToVisible:view1.frame animated:YES];
} else {
[innerScroll scrollRectToVisible:view2.frame animated:YES];
}
}
}
并在内部 UIScrollView 尚未到达最后一个视图时禁用和启用父 UIScrollViews。
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if([scrollView isEqual:innerScroll]){
if(CGRectIntersectsRect(scrollView.bounds, view1.frame)){
if(CGRectIntersectsRect(filterScroll.bounds, view11.frame)){
} else if(CGRectIntersectsRect(filterScroll.bounds, view22.frame)){
}
mainScroll.scrollEnabled = NO;
} else if (CGRectIntersectsRect(scrollView.bounds, view2.frame)){
mainScroll.scrollEnabled = YES;
}
}
}