iOS实现带遮罩的弹出选项卡

 更新时间:2020年6月30日 23:42  点击:1788

在我们日常开发的过程中难免会碰到一些选项的需求,下面是我针对我们该次需求做的一个小的Demo,闲话不多说了,上图片,上代码。

这样在我们选择上面一个Cell进行点击的时候,我会通过一个代理把数据传递到下面的页面,下面是代码

//
// LCAlertListView.h
// MeiMeiDu
//
// Created by 韩伟佳 on 16/4/6.
// Copyright © 2016年 LangCuang. All rights reserved.
//
 
#import <UIKit/UIKit.h>
@class LCAlertListView;
 
@protocol LCAlertListViewDelegate <NSObject>
 
-(void)alertListView:(LCAlertListView*)view didSelectedRow:(NSInteger)row;
 
@end
 
@interface LCAlertListView : UIView<UITableViewDataSource, UITableViewDelegate>
 
-(instancetype)initWithFrame:(CGRect)frame datas:(NSArray*)datas;
-(instancetype)initWithFrame:(CGRect)frame datas:(NSArray*)datas count:(NSArray*)counts;
@property(nonatomic, strong) id<LCAlertListViewDelegate> delegate;
@end

下面是具体实现

//
// LCAlertListView.m
// MeiMeiDu
//
// Created by 韩伟佳 on 16/4/6.
// Copyright © 2016年 LangCuang. All rights reserved.
//
 
#import "LCAlertListView.h"
#import "NoFreeCell.h"
 
static CGFloat TableViewHeight ;
 
@implementation LCAlertListView{
 UITableView* mTableView;
 NSArray* tableData;
 NSArray* visiableData;
 NSArray* visiableCount;
 UIButton* backgroundBtn;
}
 
-(instancetype)initWithFrame:(CGRect)frame datas:(NSArray*)datas{
 if (self = [super initWithFrame:frame]) {
 self.backgroundColor = [UIColor clearColor];
 backgroundBtn = [[UIButton alloc] initWithFrame:frame];
 backgroundBtn.backgroundColor = RGBA(88, 88, 88, 0.8);
 [backgroundBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:backgroundBtn];
 tableData = datas;
 TableViewHeight = (datas.count + 1) * 44 + 20;
 mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, TableViewHeight) style:UITableViewStylePlain];
 [mTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
 mTableView.delegate = self;
 mTableView.dataSource = self;
 [self addSubview:mTableView];
 [UIView animateWithDuration:.25 animations:^{
 [mTableView setFrame:CGRectMake(0, kScreenHeight - TableViewHeight, kScreenWidth, TableViewHeight)];
 } completion:^(BOOL finished) {
 
 }];
 }
 return self;
}
-(instancetype)initWithFrame:(CGRect)frame datas:(NSArray*)datas count:(NSArray*)counts{
 if (self = [super initWithFrame:frame]) {
 self.backgroundColor = [UIColor clearColor];
 backgroundBtn = [[UIButton alloc] initWithFrame:frame];
 backgroundBtn.backgroundColor = RGBA(88, 88, 88, 0.8);
 [backgroundBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:backgroundBtn];
 visiableData = datas;
 visiableCount = counts;
 TableViewHeight = (datas.count + 1) * 44 + 20;
 mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, TableViewHeight) style:UITableViewStylePlain];
 [mTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
 mTableView.delegate = self;
 mTableView.dataSource = self;
 [self addSubview:mTableView];
 [UIView animateWithDuration:.25 animations:^{
 [mTableView setFrame:CGRectMake(0, kScreenHeight - TableViewHeight, kScreenWidth, TableViewHeight)];
 } completion:^(BOOL finished) {
 
 }];
 }
 return self;
}
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 if(tableData.count > 0){
 return [tableData count];
 }else if (visiableCount.count > 0){
 return [visiableCount count];
 }
 return nil;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 UITableViewCell* cell;
 NoFreeCell *doubleCell;
 if([tableData count] <= 3 && [tableData count] > 0){
 cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
 cell.textLabel.text = tableData[indexPath.row];
 return cell;
 
 }else {
 static NSString *identifier = @"cell0";
 doubleCell =[tableView dequeueReusableCellWithIdentifier:identifier];
 if (doubleCell == nil){
 doubleCell= [[NoFreeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
 doubleCell.visibleRoleLabel.text = visiableData[indexPath.row];
 doubleCell.showVisibleRoleLabel.text = visiableCount[indexPath.row];
 }
 return doubleCell;
 }
 
}
 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 NSInteger row = indexPath.row;
 [self dismiss:row];
}
 
-(void)dismiss:(NSInteger) row{
 if (_delegate && [_delegate respondsToSelector:@selector(alertListView:didSelectedRow:)]) {
 [_delegate alertListView:self didSelectedRow:row];
 }
 
 [UIView animateWithDuration:.15 animations:^{
 [mTableView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, TableViewHeight)];
 } completion:^(BOOL finished) {
 [self removeFromSuperview];
 }];
 
}
 
-(void)dismiss{
 [UIView animateWithDuration:.15 animations:^{
 [mTableView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, TableViewHeight)];
 } completion:^(BOOL finished) {
 [self removeFromSuperview];
 }];
}
@end

上面的NoFree 文件只是一个自定义的Cell,我们可以根据自己的需求自己设计,就不上传了,最后我们说说用法:

LCAlertListView* alertListView = [[LCAlertListView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) datas:visibleRoleArray count:visibleRoleCountArray];
 alertListView.delegate = self;
 [[[self.view superview] superview] addSubview:alertListView];

下面是代理传值的使用

#pragma mark - LCAlertListViewDelegate
-(void)alertListView:(LCAlertListView *)view didSelectedRow:(NSInteger)row{
 if(didSelectedIndex == 0){
 testVisibleRole = visibleRoleArray[row];
 }else{
 testData = datas[row];
 }
 
 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:didSelectedIndex inSection:0];
 [_myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

这样,我们的AlertTableVIew 就做好了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • iOS APP h5快捷程序 .mobileconfig的生成

    1.从APP Store 下载Apple Configurator 2从一个管理点管理所有iOS设备应用程序,文档和配置文件。想要确保您的所有家庭成员在其每台iOS设备上都有类似的应用和文档,管理日益增...2021-12-23
  • iOS设置UIButton文字显示位置和字体大小、颜色的方法

    这篇文章给大家分享了iOS如何设置UIButton的文字显示位置和字体的大小、颜色,文中给出了示例代码,相信对大家的学习和理解很有帮助,有需要的朋友们下面来一起看看吧。...2020-06-30
  • iOS如何将图片裁剪成圆形

    这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • iOS给border设置渐变色的方法实例

    这篇文章主要给大家介绍了关于iOS给border设置渐变色的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
  • iOS新版微信底部返回横条问题的解决

    这篇文章主要介绍了iOS新版微信底部返回横条问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • 基于Ionic3实现选项卡切换并重新加载echarts

    这篇文章主要介绍了基于Ionic3实现选项卡切换并重新加载echarts,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-24
  • vue+axios全局添加请求头和参数操作

    这篇文章主要介绍了vue+axios全局添加请求头和参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-24
  • iOS蓝牙设备名称缓存问题的解决方法

    这篇文章主要给大家介绍了关于iOS蓝牙设备名称缓存问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • iOS新版微信底部工具栏遮挡问题完美解决

    这篇文章主要介绍了iOS新版微信底部工具栏遮挡问题完美解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • Bootstrap选项卡学习笔记分享

    这篇文章主要为大家详细介绍了Bootstrap选项卡学习笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-02-19
  • iOS新增绘制圆的方法实例代码

    这篇文章主要给大家介绍了关于iOS新增绘制圆的方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-30
  • JS+CSS实现仿msn风格选项卡效果代码

    本文实例讲述了JS+CSS实现仿msn风格选项卡效果代码。分享给大家供大家参考,具体如下:这是一款来自MSN网站的选项卡,清新淡蓝色风格,新闻类、资讯类、文章类网站都比较适合使用,代码扩展性高,W3C标准设计,兼容IE、火狐等多种...2015-10-23
  • iOS UIBezierPath实现饼状图

    这篇文章主要为大家详细介绍了iOS UIBezierPath实现饼状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-20
  • 封装 axios+promise通用请求函数操作

    这篇文章主要介绍了封装 axios+promise通用请求函数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
  • IOS获取各种文件目录路径的方法

    ios获取文件路径的方法,iphone沙箱模型的四个文件夹,通过documents,tmp,app,Library得到模拟器路径的简单方式,下面小编整理相关资料,把IOS获取各种文件目录路径的方式总结如下,需要的朋友可以参考下...2020-06-30
  • Vue中 axios delete请求参数操作

    这篇文章主要介绍了Vue中 axios delete请求参数操作,具有很好的参考价值,希望对大家有所 帮助。一起跟随小编过来看看吧...2020-08-26
  • iOS UICollectionView实现卡片效果

    这篇文章主要为大家详细介绍了iOS UICollectionView实现卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • 利用CSS3制作漂亮TAB选项卡方法介绍

    TAB选项卡是一个非常不错可以在同样大小的空间中放更多的内容了,现在我们看很多门户网站都有这个功能,可能大家见得最多的就是QQ弹窗了,有很多的分类可以进行切换的哦。...2017-07-06
  • 简单实现js选项卡切换效果

    这篇文章主要为大家介绍了简单实现js选项卡切换效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-02-05