概述
自定義蒙層彈起View,點擊一下遮罩或界面上關閉按鈕,頁面會自動下去(從上向下)
詳細
在一些少數據沒必要跳轉下個界面,我們的產品大大就設計了在當前界面的底部彈上來一個View!
看下項目里截圖:
一、主要思路
1、首先封裝這個自定義蒙層彈起View: ZLBounceView
2、在ZLTuanNumView里添加你需要的視圖 View
3、使用代理和模型傳值
二、程序實現
Step1. 首先封裝這個自定義蒙層彈起View: ZLBounceView
設置界面相關:
- (void)setupContent { self.frame = CGRectMake(0, 0, UI_View_Width, ZLBounceViewHight); //alpha 0.0 白色 alpha 1 :黑色 alpha 0~1 :遮罩顏色,逐漸 self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]; self.userInteractionEnabled = YES; [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]]; if (_contentView == nil) { _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, UI_View_Height - ZLTuanNumViewHight, UI_View_Width, ZLBounceViewHight)]; _contentView.backgroundColor = [UIColor whiteColor]; [self addSubview:_contentView]; // 右上角關閉按鈕 UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; closeBtn.frame = CGRectMake(_contentView.width - 20 - 15, 15, 20, 20); [closeBtn setImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateNormal]; [closeBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:closeBtn]; } }
展示從底部向上彈出的UIView(包含遮罩):
- (void)showInView:(UIView *)view { if (!view) { return; } [view addSubview:self]; [view addSubview:_contentView]; [_contentView setFrame:CGRectMake(0, UI_View_Height, UI_View_Width, ZLBounceViewHight)]; [UIView animateWithDuration:0.3 animations:^{ self.alpha = 1.0; [_contentView setFrame:CGRectMake(0, UI_View_Height - ZLBounceViewHight, UI_View_Width, ZLBounceViewHight)]; } completion:nil]; }
移除從上向底部彈下去的UIView(包含遮罩):
- (void)disMissView { [_contentView setFrame:CGRectMake(0, UI_View_Height - ZLBounceViewHight, UI_View_Width, ZLBounceViewHight)]; [UIView animateWithDuration:0.3f animations:^{ self.alpha = 0.0; [_contentView setFrame:CGRectMake(0, UI_View_Height, UI_View_Width, ZLBounceViewHight)]; } completion:^(BOOL finished){ [self removeFromSuperview]; [_contentView removeFromSuperview]; }]; }
.h 文件里露出方法:
//展示從底部向上彈出的UIView(包含遮罩) - (void)showInView:(UIView *)view;
現在的效果圖:
Step2. 在ZLBounceView里添加你需要的視圖 View, 這里以我的 tableView 為例
<UITableViewDelegate, UITableViewDataSource>
自定義ZLBounceView:
UITableView *detailTableView = [[UITableView alloc] init]; detailTableView.backgroundColor = [UIColor clearColor]; detailTableView.frame = CGRectMake(0, CGRectGetMaxY(partner.frame), UI_View_Width, ZLBounceViewHight - tuan.frame.size.height - partner.frame.size.height - 50 - 20); [_contentView addSubview:detailTableView]; detailTableView.delegate = self; detailTableView.dataSource = self; self.detailTableView = detailTableView; self.detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UITableViewDelegate: 這里用假數據測試
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID]; cell.backgroundColor = [UIColor clearColor]; cell.textLabel.font = [UIFont systemFontOfSize:13]; cell.textLabel.textColor = ZLColor(102, 102, 102); cell.detailTextLabel.font = [UIFont systemFontOfSize:13]; cell.detailTextLabel.textColor = ZLColor(102, 102, 102); } cell.selectionStyle = UITableViewCellSelectionStyleNone; // 假數據 cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; cell.detailTextLabel.text = @"已購"; self.total.text = [NSString stringWithFormat:@"總計:%@噸", @"100"]; return cell; }
Step3. 使用代理和模型傳值
3.1 在當前ViewController中的所需按鈕,添加點擊事件
[testBtn addTarget:self action:@selector(testBtnClicked) forControlEvents:UIControlEventTouchUpInside];
3.2 添加點擊事件則為創建當前彈起View
// 詳情展開view @property (nonatomic, strong) ZLBounceView *tuanNumView;
- (void)testBtnClicked { _tuanNumView = [[ZLBounceView alloc]init]; [_tuanNumView showInView:self.view]; }
3.3 我這里使用假數據,正常情況則是請求數據或者上個界面的數據用 Model 傳過來
_tuanNumView.tuanModel = self.orderModel;
Step4. 加載從底部向上彈起的UIView; 點擊一下遮罩或界面上關閉按鈕,頁面會自動下去(從上向下)
運行效果圖如下:
三、其他補充
壓縮文件截圖:
目前是項目中直接操作, 界面性問題可以根據自己項目需求調整即可, 具體可參考代碼, 項目能夠直接運行!