美文网首页
iOS之弹窗滑动关闭

iOS之弹窗滑动关闭

作者: 我是卖报的小行家 | 来源:发表于2020-08-28 11:13 被阅读0次

常见开发中都会有这样的底部弹窗


购买卡片底部弹窗

其UI框架为scrollview->contentView(UIView)->(tableview)

UI框架

当tableview内容很多时候需要可以上下滑动,但是最外边的scrollview高度是不变的。
当我想要隐藏这个视图时候下拉会隐藏消失(此时拉动的是scrollview,而不是tableview);

上代码

 UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.layer.cornerRadius = 10;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.scrollEnabled = YES;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
            // Fallback on earlier versions
    }
    [self addSubview:scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.contentsView = ({
           UIView *contentView = [[UIView alloc]init];
           contentView.backgroundColor = MOGOBGGRAY;
           contentView.layer.cornerRadius = 10;
           contentView;
       });
    [scrollView addSubview:self.contentsView];
    [self.contentsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    self.topSlideView = ({
        UIView *aView = [[UIView alloc] init];
        aView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        aView.layer.cornerRadius = 2.5;
        aView.layer.masksToBounds = true;
        aView;
    });
    
    [self.contentsView addSubview:self.topSlideView];
    
    [self.topSlideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(15);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(5);
    }];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
    }
    self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = MOGOBGGRAY;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self.tableView registerClass:[MGMOGOS2OrderTableViewCell class] forCellReuseIdentifier:cellIdentifier0];
    [self.tableView registerClass:[MGMOGOS2ManagerPackageTableViewCell class] forCellReuseIdentifier:cellIdentifier1];
    [self.tableView registerClass:[MGMOGOS2ManagerAutoPackageTableViewCell class] forCellReuseIdentifier:cellIdentifier2];
    [self.contentsView addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topSlideView.mas_bottom).offset(16 * MScreenScaleY);
        make.left.right.equalTo(self.contentsView);
        make.height.mas_equalTo(400 * MScreenScaleY);
    }];
    
    self.bottomConfirmView = [[PackageDetailCustomView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, 64)];
    self.bottomConfirmView.backgroundColor = self.tableView.backgroundColor;
    self.bottomConfirmView.delegate = self;
    self.bottomConfirmView.isConfirmPay = YES;
    self.bottomConfirmView.confirmLab.text =NSLocalizedString(@"store_packdetail_confirm_submit", nil);
    [self.contentsView addSubview:self.bottomConfirmView];
    self.bottomConfirmView.autoBoostDetailLabel.hidden = YES;
    
    [self.bottomConfirmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentsView);
        make.top.equalTo(self.tableView.mas_bottom);
        make.height.mas_offset(64 + tabBarBottomChinHeight);
        make.bottom.equalTo(self.contentsView);
    }];

然后需要更新tableview高度时候再更新下tableview高度

[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                       make.height.mas_equalTo(xxxxxx);
 }];

重点代码

//设置scrollview和tableview禁止滑动时候不用scrollEnable,而是用其偏移量。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.tableView]) {
        if (scrollView.contentOffset.y <= 0) {
            self.scrollView.contentOffset = CGPointMake(0,scrollView.contentOffset.y);
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0) ;
        }

        if (scrollView.contentSize.height - scrollView.contentOffset.y-scrollView.frame.size.height <=  0){
            self.scrollView.contentOffset = CGPointMake(0, 0);
            scrollView.contentOffset = CGPointMake(0,scrollView.contentOffset.y) ;
        }
    }else{
        //禁止 scrollview向上滑动
        if (scrollView.contentOffset.y >0) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
        }
    }
}

拖动结束时候再隐藏view

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.delegate &&[self.delegate respondsToSelector:@selector(closeViewWithSlipDistance:)]) {
        [self.delegate closeViewWithSlipDistance:scrollView.contentOffset.y];
    }
}

相关文章

网友评论

      本文标题:iOS之弹窗滑动关闭

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