項(xiàng)目中是三層結(jié)構(gòu)
ScrollVIew -> WMPageController -> UITableView(UICollectionView)
子頁(yè)面邏輯不多贅述舆乔,主要說(shuō)明多層級(jí)聯(lián)合滾動(dòng)判定
父頁(yè)面ScrollView代碼
// 聲明一個(gè)HXScrollView 繼承自UIScrollView
@interface HXScrollView : UIScrollView <UIGestureRecognizerDelegate>
@end
@implementation HXScrollView
// 實(shí)現(xiàn)文件添加下面方法啟動(dòng)聯(lián)動(dòng)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end
接下來(lái)在要做聯(lián)動(dòng)的控制器內(nèi)實(shí)現(xiàn)HXScrollView的實(shí)例對(duì)象涂籽,并在聲明文件內(nèi)創(chuàng)建一個(gè)BOOL屬性對(duì)象對(duì)scrollview是否可以滾動(dòng)進(jìn)行控制
@property (nonatomic, assign)BOOL superCanScroll; // mainScrollView能否滾動(dòng)
// 子頁(yè)面內(nèi)滾動(dòng)判定回調(diào)滾動(dòng)屬性
self.expertVC = HXStrategyExpertVC.new;
self.expertVC.block = ^(BOOL isScroll) {
weakSelf.superCanScroll = isScroll;
};
vc = self.expertVC;
#pragma mark - 滾動(dòng)處理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.scrollView) {
CGFloat maxOffset = ScreenWidth * 0.32;
if (!self.superCanScroll) {
CGPoint point = self.scrollView.contentOffset;
point.y = maxOffset;
NSLog(@"contentOffset %.1f",scrollView.contentOffset.y);
self.scrollView.contentOffset = point;
// self.expertVC.subviewCanScroll = YES; // 對(duì)相應(yīng)的子級(jí)頁(yè)面創(chuàng)建同樣的控制屬性
}else {
NSLog(@"contentOffset %.1f",scrollView.contentOffset.y);
if (scrollView.contentOffset.y >= maxOffset - 5) {
CGPoint point = scrollView.contentOffset;
point.y = maxOffset;
scrollView.contentOffset = point;
self.superCanScroll = NO;
}
// self.expertVC.subviewCanScroll = YES;
}
}
}
子級(jí)頁(yè)面代碼
@property (nonatomic, copy)BannerScrollBlock block; // 反饋滾動(dòng)視圖是否聯(lián)動(dòng)
@property (nonatomic, assign)BOOL subviewCanScroll; // 列表可滑動(dòng)
#pragma 首頁(yè)滾動(dòng)圖聯(lián)動(dòng)判斷
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!self.subviewCanScroll) {
CGFloat y = scrollView.contentOffset.y;
if (y < 0) {
}else {
CGPoint point = scrollView.contentOffset;
point.y = 0;
scrollView.contentOffset = point;
}
}else {
if (scrollView.contentOffset.y <= 0) {
self.subviewCanScroll = NO;
if (self.block) {
self.block(YES);
}
}
}
}