美文网首页
iOS 简单列表弹窗

iOS 简单列表弹窗

作者: 邂逅阳光 | 来源:发表于2022-07-04 10:18 被阅读0次

    简单封装的列表弹窗 暂时无法实现滚动 如需滚动把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;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 简单列表弹窗

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