UIScrollView嵌套滚动方案

预期效果

如图1所示:
UIScrollView进行嵌套组合,最外层是一个UIScrollView,为了说明方便,称为父UIScrollView
里面嵌套着头部区、悬停区和一个UIScrollView,称为子UIScrollView

图1

要求头部区展示在屏幕时,滚动内容为父UIScrollView,一旦头部区域消失在屏幕中时,这个时候滚动的内容为子UIScrollView,悬停区悬停在屏幕顶部。如图2 所示


图2

解决方案

方案1

通过监听父UIScrollView的contentOffset,如果偏移量小于于头部区的高度,则设置父UIScrollView的scrollEnabled为YES,设置子UIScrollView的scrollEnable为NO。如果偏移量大于头部区的高度,则设置父UIScrollView的scrollEnabled为NO,设置子UIScrollView的scrollEnable为YES.
这个方案存在最大的问题是一旦快速滑动屏幕,到临界值那边会卡住,需要重新施加一次滑动手势,才能继续滚动。

方案2

让一次手势同时拥有多个响应者,即一次滑动手势无论子UIScrollView或者父UIScrollView都能作为响应者进行滚动,然后我们根据自己的需要进行重置不需要的滚动效果。
要达到手势能够被多个UIView进行响应,需要在父UIScrollView实现 UIGestureRecognizerDelegate 代理方法

//支持手势同步到其他接收者
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

在他们所在的父View或ViewController中实现 UIScrollViewDelegate 的

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

默认只能滚动父UIScrollView,子UIScrollView在未被允许滚动的情况,偏移量会被重置为0。 比对父UIScrollView的偏移量和头部区的高度,如果偏移量大于等于头部区的高度,则开始重置父头部区的高度的偏移量为头部区高度,子UIScrollView不再重置偏移量为0.

具体代码实现

定义一个父UIScrollView为 FatherScrollView

// FatherScrollView.h
#import <UIKit/UIKit.h>

@interface FatherScrollView : UIScrollView<UIGestureRecognizerDelegate>

@end


//FatherScrollView.m
#import "FatherScrollView.h"

@implementation FatherScrollView

//支持手势同步到其他接收者
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end

定义一个ViewController进行内容展示

#import "ViewController.h"
#import "FatherScrollView.h"

@interface ViewController ()<UIScrollViewDelegate>
//父UIScrollView
@property (nonatomic, strong) FatherScrollView *fatherscrollView;
//头部区
@property (nonatomic, strong) UIView *headerView;
//子UIScrollView
@property (nonatomic, strong) UIScrollView *childScrollView;
//父UIScrollView是否能滚动
@property (nonatomic, assign) BOOL enableFatherViewScroll;
//子UIScrollView是否能滚动
@property (nonatomic, assign) BOOL enableChildViewScroll;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.enableChildViewScroll = NO;
    self.enableFatherViewScroll = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.fatherscrollView = [[FatherScrollView alloc] initWithFrame:self.view.bounds];
    self.fatherscrollView.delegate = self;
    self.fatherscrollView.bounces = YES;
    self.fatherscrollView.backgroundColor = UIColor.grayColor;
    self.fatherscrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height+200);
    [self.view addSubview:self.fatherscrollView];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    self.headerView.backgroundColor = UIColor.greenColor;
    [self.fatherscrollView addSubview:self.headerView];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"悬浮标题";
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(0, 160, self.view.bounds.size.width, 40);
    label.backgroundColor = UIColor.whiteColor;
    [self.headerView addSubview:label];
    
    self.childScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.childScrollView.delegate = self;
    self.childScrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height * 1.5);
    self.childScrollView.backgroundColor = UIColor.redColor;
    [self.fatherscrollView addSubview:self.childScrollView];
    
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.font = [UIFont systemFontOfSize:50];
    contentLabel.text = @"子滚动内容";
    contentLabel.textAlignment = NSTextAlignmentCenter;
    contentLabel.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    [self.childScrollView addSubview:contentLabel];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat contentOffset = 160 - [UIApplication sharedApplication].statusBarFrame.size.height;
    if (scrollView == self.fatherscrollView) {
        if (!self.enableFatherViewScroll) {
            scrollView.contentOffset = CGPointMake(0, contentOffset);
            self.enableChildViewScroll = YES;
        } else {
            if (scrollView.contentOffset.y >= contentOffset) {
                scrollView.contentOffset = CGPointMake(0, contentOffset);
                if (self.enableFatherViewScroll) {
                    self.enableFatherViewScroll = NO;
                    self.enableChildViewScroll = YES;
                }
            }
        }
    } else {
        if (!self.enableChildViewScroll) {
            scrollView.contentOffset = CGPointMake(0, 0);
        } else {
            if (scrollView.contentOffset.y <= 0) {
                self.enableChildViewScroll = NO;
                self.enableFatherViewScroll = YES;
            }
        }
    }
}

@end
开发中的问题

1、这种嵌套的设计,往往会包含有多个子UIScrollView并且可以进行左右滑动切换,子UIScrollView区域往往会当独封闭,如果都在UIViewController这边进行处理的话,会有大量的耦合代码出现
2、不得不为父UIScrollView创建一个新的UIScrollView在子类,实现 (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

针对上面问题,我这边对UIScrollView嵌套方案进行了简要的封装,方便开发中直接使用
Demo地址:CCNestScrollView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容