iOS 简单列表弹窗

简单封装的列表弹窗 暂时无法实现滚动 如需滚动把BGVIew换成scrollView即可
可固定数据源弹窗亦可自定义数据源弹窗
有不妥之处请各位大佬不吝赐教 抱拳了


341656900049_.pic.jpg
351656900070_.pic.jpeg

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
typedef enum : NSUInteger {
    ///买卖样式
    alertStyleBuyAndSell,
    ///上下架样式
    alertStyleOnShelves,
    ///时间 15min、6hours样式
    alertStyleTime,
} listAlertStyle;

typedef enum : NSUInteger {
    ///居左
    contentAlignmentLeft,
    ///居中
    contentAlignmentCenter,
    ///居右
    contentAlignmentRight
} contentAlignment;
/// 选择回调 并返回当前选择的index 和内容
typedef void(^selectIndexClick)(NSInteger index,NSString *selectText);
/// 取消回调
typedef void(^cancelClick)(void);

@interface CIListAlertView : UIView
///  展示列表弹窗
/// @param style 样式
/// @param selectItemText 默认选中的文本
/// @param selectIndex 选中回调
/// @param cancle 取消回调
+ (void)showListAlertWithStyle:(listAlertStyle)style
                selectItemText:(NSString *_Nullable)selectItemText
                   selectIndex:(selectIndexClick)selectIndex
                        cancle:(cancelClick)cancle;
///  自定义弹窗数据
/// @param dataArr 数据源
/// @param contentAlignment 对齐方式
/// @param selectItemText 默认选中的文本
/// @param selectIndex 选中回调
/// @param cancle 取消回调
+ (void)showListAlertWithDataArr:(NSArray <NSString *>*)dataArr
                contentAlignment:(contentAlignment)contentAlignment
                  selectItemText:(NSString *_Nullable)selectItemText
                     selectIndex:(selectIndexClick)selectIndex
                          cancle:(cancelClick)cancle;
@end

.m文件

@interface CIListAlertView ()
/// btn数组
@property (nonatomic, strong) NSMutableArray *itemArr;
/// 按钮文本数组
@property (nonatomic, strong) NSMutableArray *itemStrArr;
/// 按钮内容居左还是居中
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
/// 对钩icon
@property (nonatomic, strong) UIImageView *hookIcon;
/// 选中回调
@property (nonatomic, copy) selectIndexClick select;
/// 取消回调
@property (nonatomic, copy) cancelClick cancle;
/// 弹框样式
@property (nonatomic, assign) listAlertStyle style;
/// 默认选中的item
@property (nonatomic, copy) NSString *selectItemText;
/// 自定义数据源
@property (nonatomic, strong) NSArray *customDataArr;
/// 自定义对齐方式
@property (nonatomic, assign) contentAlignment contentAlignmentStyle;
@end

@implementation CIListAlertView
+ (void)showListAlertWithStyle:(listAlertStyle)style
                selectItemText:(NSString *_Nullable)selectItemText
                   selectIndex:(selectIndexClick)selectIndex
                        cancle:(cancelClick)cancle {
    if (style > 5) { //如果状态不对则不展示
        return;
    }
    CIListAlertView *alertView = [[CIListAlertView alloc] initWithStyle:style
                                                                DataArr:nil
                                                       contentAlignment:0
                                                         selectItemText:selectItemText
                                                            selectIndex:selectIndex
                                                                 cancle:cancle];
    [[[UIApplication sharedApplication] delegate].window addSubview:alertView];
    [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
+ (void)showListAlertWithDataArr:(NSArray <NSString *>*)dataArr
                contentAlignment:(contentAlignment)contentAlignment
                  selectItemText:(NSString *_Nullable)selectItemText
                     selectIndex:(selectIndexClick)selectIndex
                          cancle:(cancelClick)cancle {
    if ([dataArr count] == 0) {
        return;
    }
    CIListAlertView *alertView = [[CIListAlertView alloc] initWithStyle:0
                                                                DataArr:dataArr
                                                       contentAlignment:contentAlignment
                                                         selectItemText:selectItemText
                                                            selectIndex:selectIndex
                                                                 cancle:cancle];
    [[[UIApplication sharedApplication] delegate].window addSubview:alertView];
    [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
- (instancetype)initWithStyle:(listAlertStyle)style
                      DataArr:(NSArray <NSString *>*_Nullable)dataArr
             contentAlignment:(contentAlignment)contentAlignment
               selectItemText:(NSString *)selectItemText
                  selectIndex:(selectIndexClick)selectIndex
                       cancle:(cancelClick)cancle {
    if (self = [super init]) {
        _select = selectIndex;
        _cancle = cancle;
        _style = style;
        _contentAlignmentStyle = contentAlignment;
        _selectItemText = selectItemText;
        _customDataArr = dataArr;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [self addGestureRecognizer:tap];
        if ([dataArr count] == 0) {
            [self loadData];
        } else {
            [self customData];
        }
        [self createUI];
    }
    return self;
}
#pragma mark - 自定义数据源
- (void)customData {
    [self.itemStrArr setArray:self.customDataArr];
    [self.itemStrArr addObject:@"取消"];
    if (![self.itemStrArr containsObject:self.selectItemText] || [self.selectItemText length] == 0 || self.selectItemText == nil) {
        self.selectItemText = [self.itemStrArr firstObject];
    }
    switch (_contentAlignmentStyle) {
        case contentAlignmentLeft:{
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
            break;
        case contentAlignmentCenter:{
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
        case contentAlignmentRight:{
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        }
            break;
        default:
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            break;
    }
}
#pragma mark - 加载数据源
- (void)loadData {
    switch (_style) {
        case alertStyleBuyAndSell:{//买卖样式
            [self.itemStrArr addObject:@"所有类型"];
            [self.itemStrArr addObject:@"购买"];
            [self.itemStrArr addObject:@"出售"];
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
            break;
        case alertStyleOnShelves:{//上下架样式
            [self.itemStrArr addObject:@"所有状态"];
            [self.itemStrArr addObject:@"上架"];
            [self.itemStrArr addObject:@"下架"];
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
            break;
        case alertStyleTime:{//时间 15min、6hours样式
            [self.itemStrArr addObject:@"15 Min"];
            [self.itemStrArr addObject:@"30 Min"];
            [self.itemStrArr addObject:@"45 Min"];
            [self.itemStrArr addObject:@"1 Hours"];
            [self.itemStrArr addObject:@"2 Hours"];
            [self.itemStrArr addObject:@"3 Hours"];
            [self.itemStrArr addObject:@"6 Hours"];
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
            
        default:
            break;
    }
    [self.itemStrArr addObject:@"取消"];
    //判断默认选中文本是否存在数组中 不存在则默认选中第一个item
    if (![self.itemStrArr containsObject:self.selectItemText] || [self.selectItemText length] == 0 || self.selectItemText == nil || [self.selectItemText isEqualToString:[self.itemStrArr lastObject]]) {
        self.selectItemText = [self.itemStrArr firstObject];
    }
}
#pragma mark - 创建UI
- (void)createUI {
    UIView *bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-36);
    }];
    if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
        self.hookIcon.frame = CGRectMake(32, 21, 12, 10);
    }
    self.hookIcon.hidden = self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentCenter;//如果样式居中则隐藏否则展示
    [bgView addSubview:self.hookIcon];
    
    UIButton *oldBtn;
    for (int i = 0; i < [self.itemStrArr count]; i ++) {
        NSString *itemStr = [self.itemStrArr objectAtIndex:i];
        UIButton *itemBtn = [[UIButton alloc]init];
        [itemBtn setTitle:itemStr forState:UIControlStateNormal];
        if (i == [self.itemStrArr count] - 1) {//取消按钮颜色
            [itemBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        } else {
            [itemBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [itemBtn setTitleColor:[UIColor colorWithRed:130/255.0 green:92/255.0 blue:255/255.0 alpha:1] forState:UIControlStateSelected];
        }
        if ([self.selectItemText isEqualToString:itemStr]) {
            itemBtn.selected = YES;
        }
        itemBtn.tag = i + 500;
        [itemBtn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        itemBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        itemBtn.contentHorizontalAlignment = self.contentHorizontalAlignment;
        [bgView addSubview:itemBtn];
        [itemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.height.mas_equalTo(52);
            make.right.mas_equalTo(-16);
            if (i == 0) {
                make.top.mas_equalTo(0);
            } else {
                make.top.mas_equalTo(oldBtn.mas_bottom).mas_offset(0);
            }
            if (i == [self.itemStrArr count] - 1) {
                make.bottom.mas_equalTo(0);
            }
        }];
        oldBtn = itemBtn;
        
        if (i != [self.itemStrArr count] - 1) {//不为最后一条都得加分割线
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.3];
            [bgView addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
                make.height.mas_equalTo(0.5);
                make.top.mas_equalTo(itemBtn.mas_bottom).mas_offset(0);
            }];
        }
    }
    NSInteger index =[self.itemStrArr indexOfObject:self.selectItemText] ;
    self.hookIcon.centerY = 52 * index + 26;//每一行的高度*itemIndex再加当前行高度的一半
}
#pragma mark - 选中事件
- (void)itemClick:(UIButton *)sender {
    if (sender.tag == [self.itemStrArr count] + 500 - 1) {//点击取消
        if (_cancle) {
            _cancle();
        }
        [self dismiss];
    } else {
        MJWeakSelf
        if (weakSelf.select) {
            weakSelf.select(sender.tag - 500,weakSelf.itemStrArr[sender.tag - 500]);
        }
        [weakSelf dismiss];
    }
    self.hookIcon.centerY = sender.centerY;
}
- (void)dismiss {
    [self removeFromSuperview];
}

#pragma mark - 懒加载
- (NSMutableArray *)itemStrArr {
    if (!_itemStrArr) {
        _itemStrArr = [[NSMutableArray alloc]init];
    }
    return _itemStrArr;
}
- (UIImageView *)hookIcon {
    if (!_hookIcon) {
        _hookIcon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 21, 12, 10)];
        _hookIcon.image = [UIImage imageNamed:@"dark_select"];
        _hookIcon.hidden = YES;
    }
    return _hookIcon;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容