在一些少數據不必跳轉下個界面,咱們的產品大大就設計了在當前界面的底部彈上來一個View!html
看下項目裏截圖:ide
一、首先封裝這個自定義蒙層彈起View: ZLBounceView測試
二、在ZLTuanNumView裏添加你須要的視圖 Viewatom
三、使用代理和模型傳值spa
Step1. 首先封裝這個自定義蒙層彈起View: ZLBounceView設計
設置界面相關:3d
- (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(包含遮罩):code
- (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 文件裏露出方法:orm
//展現從底部向上彈出的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; 點擊一下遮罩或界面上關閉按鈕,頁面會自動下去(從上向下)
運行效果圖以下:
壓縮文件截圖:
目前是項目中直接操做, 界面性問題能夠根據本身項目需求調整便可, 具體可參考代碼, 項目可以直接運行!