简单的联划容器(两个scroll嵌套)

两个scroll嵌套的scrollview联划的方案,通过contentInset的形式,为底部的scroll开辟空间,监听顶部的滑动、模拟底部的滑动,具体代码如下


image.png

.h

#import 
#import "LYCommonScrollView.h"
NS_ASSUME_NONNULL_BEGIN
//上下嵌套的scrollView 联滑
@protocol  LYMixNestScrollContainerViewScrollDelegate
-(void)containerViewDidScroll:(UIScrollView *)scrollView;
@optional

-(void)didMoveToPage:(NSInteger)pageIdx mainScroll:(UIScrollView *)mainScroll bottomScroll:(UIScrollView * _Nullable )bottomScroll;

@end




@interface LYMixNestScrollContainerView : UIView
//main Scroll mjheader和、backtoTopButton都附加到这里
@property (nonatomic, strong,readonly) LYCommonScrollView *scrollView;
@property (nonatomic, weak) id scrollDelegate;
- (instancetype)initWithFrame:(CGRect)frame topView:(UIScrollView *)topView bottomView:(UIScrollView *)bottomView;
- (CGFloat)topContentHeight;
-(void)setShiedTouchFrame:(CGRect)frame;
@end

NS_ASSUME_NONNULL_END

.m

//
//  lyMixNestScrollView.m
//  lyEjGameDemo
//
//

#import "MixNestScrollContainerView.h"
#import "UIView+ly.h"
#import "lyConstants.h"
#import "FBKVOController.h"
#import "WeakifyStrongify.h"
#import 
#import "MJRefresh.h"
#import "lyDeviceUtil.h"
#import "lyABTest.h"

@interface MixNestScrollContainerView()

@property (nonatomic, strong) UIScrollView *topView;
@property (nonatomic, strong) UIScrollView *bottomView;
@property (nonatomic,assign) CGRect shieldFrame;


@end

@implementation MixNestScrollContainerView


- (instancetype)initWithFrame:(CGRect)frame topView:(UIScrollView *)topView bottomView:(UIScrollView *)bottomView
{
    self = [super initWithFrame:frame];
    if (self) {
        self.topView = topView;
        self.bottomView = bottomView;
        [self setup];
    }
    return self;
}


- (CGFloat)topContentHeight {
    return self.topView.contentSize.height;
}

- (UIScrollView *)scrollView {
    return _topView;
}


-(void)layoutSubviews {
    [super layoutSubviews];
    self.topView.ly_top = 0;
    if(self.topView.ly_height != self.ly_height) {
        self.topView.ly_height = self.ly_height;
    }
    if(self.bottomView.ly_height != self.ly_height) {
        self.bottomView.ly_height = self.ly_height;
    }

    [self updateBottomContentInset:self.bottomView.contentInset.bottom];

}



-(void)reloadSubScroll:(UIScrollView *)scrollView {
    if([scrollView isKindOfClass:UICollectionView.class]) {
        [(UICollectionView *)scrollView reloadData];
    } else if ([scrollView isKindOfClass:UITableView.class]) {
        [(UITableView *)scrollView reloadData];

    }
}

-(CGFloat)calSizeHeightForScroll:(UIScrollView *)view {
    if([view isKindOfClass:UICollectionView.class]) {
        UICollectionView *collection = (UICollectionView *)view;
        return collection.collectionViewLayout.collectionViewContentSize.height;
    }
    
    return view.contentSize.height;
     
}

-(void)setup {
    [self addSubview:self.scrollView];
    self.bottomView.scrollEnabled = NO;

    [self.topView addSubview:self.bottomView];
    
    [self.KVOController unobserve:_topView];
    @weakify(self);
    [self.KVOController observe:_topView
                       keyPaths:@[@"contentSize",@"contentOffset"]
                        options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
                          block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary * _Nonnull change) {
        @strongify(self);
        [self updateScrollls];
        [self.scrollDelegate containerViewDidScroll:self.topView];
    }];
    [self.KVOController unobserve:_bottomView];
    [self.KVOController observe:_bottomView
                        keyPath:@"contentSize"
                        options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
                          block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary * _Nonnull change) {
        @strongify(self);
        [self layoutScrollIfNeeded];
    }];


}


-(void)layoutScrollIfNeeded {
    if(self.topView.contentInset.bottom != self.bottomView.contentSize.height + self.bottomView.contentInset.bottom) {
        self.topView.contentInset = UIEdgeInsetsMake(self.topView.contentInset.top, self.topView.contentInset.left, self.bottomView.contentSize.height + self.bottomView.contentInset.bottom, self.topView.contentInset.right);
    }
    
}

#pragma mark - UIScrollViewDelegate

#pragma mark - private


-(void)updateScrollls {

    CGFloat offset = self.scrollView.contentOffset.y;
     [self adjustBottomCollection:offset];

}
-(void)adjustBottomCollection:(CGFloat )offset {
    
    CGFloat top = self.topView.contentSize.height;
    if(offset >= top) {
        self.bottomView.ly_top = offset;
        [self.bottomView setContentOffset:CGPointMake(0, offset - top)];
        
    } else {
        
        self.bottomView.ly_top = top;
        [self.bottomView setContentOffset:CGPointMake(0,0)];
    }
    
    
 //   NSLog(@"scroll BottomCollection %f  bottomViewsize %f offset  %f",self.bottomView.contentOffset.y,self.bottomView.contentSize.height,offset - self.topView.contentSize.height);

  //  NSLog(@"scroll BottomCollection %f  offset  %f",self.bottomView.contentOffset.y,offset - self.topView.contentSize.height);

}

- (void)updateBottomContentInset:(CGFloat)bottom{
    // 解决mjFooter不悬停的问题
    if(bottom > 0 &&  bottom != self.scrollView.contentInset.bottom - self.bottomView.contentSize.height) {
        self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left,self.bottomView.contentSize.height + bottom, self.scrollView.contentInset.right);
    }
}




- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if(CGRectContainsPoint(self.shieldFrame, point)) {
        return NO;
    } else {
        return [super pointInside:point withEvent:event];
    }

}


- (void)setShiedTouchFrame:(CGRect)frame {
    self.shieldFrame = frame;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

你可能感兴趣的:(简单的联划容器(两个scroll嵌套))