美文网首页
多级UIScrollView嵌套-简易版本

多级UIScrollView嵌套-简易版本

作者: 守护地中海的花 | 来源:发表于2019-05-28 11:29 被阅读0次

多级UIScrollView嵌套-升级版本

  • 效果


    QQ20210104-175404.gif
  • 核心思路

首先UIScrollView嵌套核心就是一个scrollView滑动 另一个停止。其ScrollView必须是允许多手势的

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
  • 描述
    一个底图 一个子图 比作A和B A和B都是继承UIScrollView


    image.png

1)首先实现滑动B联动A一起向上滑动
2)滑动一定位置A不能滑动B可以滑动
3)滑动B到顶部继续向下滑动则继续带动A滑动

A允许多手势

继承BaseScrollView,BaseScrollView允许多手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

滑动B联动A

属性 初始化是NO 子视图不能滑动来实现滑动

@property(nonatomic,assign)BOOL canScroll;

- (void)initializationScrollProperty
{
    self.canScroll = NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.canScroll == NO) {
        [scrollView setContentOffset:CGPointZero];
    }
}

设置临界点

滑动B联动A(滑动A)到一定位置允许B滑动,临界点(大于等于临界点不能滑动NO,小于临界点可以滑动)
属性

@property(nonatomic,assign)BOOL canScroll;

初始化

- (void)initializationScrollProperty
{
    self.canScroll = YES;
}

核心代码:监听A滑动某个位置通知B可以滑动了

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //设置临界点
    CGFloat maxContentOffsetY = 180;
    if (self.canScroll) {
        if (ceil(scrollView.contentOffset.y) >= ceil(maxContentOffsetY)) {
            self.canScroll = NO;
            NSLog(@"不可以滑动 发送通知到顶通知");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"arriveTop" object:nil];
        } else {
            self.canScroll = YES;
            NSLog(@"可以滑动");
        }
    } else {
        [scrollView setContentOffset:CGPointMake(0, maxContentOffsetY)];
    }
}

核心代码:B监听A到了某位置发送的通知

#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(arriveTop:) name:@"arriveTop" object:nil];
}
- (void)arriveTop:(NSNotification *)notification
{
    NSLog(@"接受到到顶通知");
    self.canScroll = YES;
}

滑动B 偏移量是负 离开顶部 这个时候B继续联动A滑动 可以向上向下

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.canScroll == NO) {
        [scrollView setContentOffset:CGPointZero];
    } else {
        if (scrollView.contentOffset.y < 0) {
            self.canScroll = NO;
            NSLog(@"离开顶部...发通知");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"leaveTop" object:nil];
        }
    }
}

核心代码:A监听B离开顶部

#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(leaveTop:) name:@"leaveTop" object:nil];
}
- (void)leaveTop:(NSNotification *)notification
{
    NSLog(@"接收到离开顶部通知");
    self.canScroll = YES;
}

完整代码

  • 子控制器
<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *mainView;
@property(nonatomic,assign)BOOL canScroll;
#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(arriveTop:) name:@"arriveTop" object:nil];
}
- (void)arriveTop:(NSNotification *)notification
{
    self.canScroll = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.canScroll == NO) {
        [scrollView setContentOffset:CGPointZero];
        self.mainView.showsVerticalScrollIndicator = NO;
    } else {
        if (scrollView.contentOffset.y < 0) {
            self.canScroll = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"leaveTop" object:nil];
        }
        self.mainView.showsVerticalScrollIndicator = YES;
    }
}
#pragma mark - lazy懒加载
- (UIScrollView *)mainView
{
    if (!_mainView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:view];
        view.delegate = self;
        view.showsVerticalScrollIndicator = NO;
        _mainView = view;
    }
    return _mainView;
}
  • 主控制器
<UIScrollViewDelegate>
@property(nonatomic,strong)BaseScrollView *mainView;
@property(nonatomic,assign)BOOL canScroll;
#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(leaveTop:) name:@"leaveTop" object:nil];
}
- (void)leaveTop:(NSNotification *)notification
{
    self.canScroll = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //设置临界点
    CGFloat maxContentOffsetY = self.sellerView.height;
    if (self.canScroll) {
        if (ceil(scrollView.contentOffset.y) >= ceil(maxContentOffsetY)) {
            self.canScroll = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"arriveTop" object:nil];
        } else {
            self.canScroll = YES;
        }
        self.mainView.showsVerticalScrollIndicator = YES;
    } else {
        [scrollView setContentOffset:CGPointMake(0, maxContentOffsetY)];
        self.mainView.showsVerticalScrollIndicator = NO;
    }
}
#pragma mark - lazy懒加载
- (UIView *)bgView
{
    if (!_bgView) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kTopHeight, WIDTH, HEIGHT - kTopHeight)];
        [self.view addSubview:view];
        view.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeVertical andWithSize:view.size andWithColors:@[(__bridge id)rgba(51, 63, 110, 1).CGColor,(__bridge id)rgba(28, 34, 64, 1).CGColor]];
        
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.width, 399*ADAPTER_WIDTH)];
        [view addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"Tender_main_2"];
        _bgView = view;
    }
    return _bgView;
}

相关文章

网友评论

      本文标题:多级UIScrollView嵌套-简易版本

      本文链接:https://www.haomeiwen.com/subject/srertctx.html