自定义PickerView

/// 选择的回调block
typedef void(^XMCustomPickerSubmitBlock) (NSInteger selectRow);

/// 自定义样式的pickerView弹框
@interface XMCustomPickerView : UIView

/// 初始化picker
- (instancetype)initWithColumDataArray:(NSArray *)columDataArray
                          selectRow:(NSInteger)selectRow
                             doneBlock:(XMCustomPickerSubmitBlock)submitBlock;

/// 弹出展示
- (void)showAction;

@end


#import "XMCustomPickerView.h"
#import "UIView+XMTool.h"

#define kCustomPickerCellHeight     40

/// 自定义样式的pickerView弹框
@interface XMCustomPickerView()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIView    *maskView;
@property (nonatomic, strong) UIView    *topBarView;
@property (nonatomic, strong) UIButton  *cancelBtn;
@property (nonatomic, strong) UILabel   *titleLbl;
@property (nonatomic, strong) UIButton  *doneBtn;
@property (nonatomic, strong) UIView    *containerView; // 容器
@property (nonatomic, copy) XMCustomPickerSubmitBlock   submitBlock;
@property (nonatomic, assign) CGFloat   contentHeight; // 容器的高度

/// 展示内容的表格
@property (nonatomic, strong) UIPickerView *pickerV;
/// 当前列表数组
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dataArray2; // 模拟第二列

@property (nonatomic, assign) NSInteger currentComponent; // 当前选中的section
@property (nonatomic, assign) NSInteger currentRow; // 当前选中的行

@end

@implementation XMCustomPickerView

- (instancetype)initWithColumDataArray:(NSArray *)columDataArray
                          selectRow:(NSInteger)selectRow
                             doneBlock:(XMCustomPickerSubmitBlock)submitBlock {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _submitBlock = submitBlock;
        self.dataArray = [NSMutableArray arrayWithArray:columDataArray];
        self.dataArray2 = [NSMutableArray arrayWithArray:columDataArray];
        self.currentRow = selectRow;
        [self initAllView];
    }
    return self;
}

- (void)initAllView {
    self.contentHeight = 408;
    self.containerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.contentHeight)];
    self.containerView.backgroundColor =  [UIColor whiteColor];
    [self addRectCorner:4 corners:UIRectCornerTopLeft|UIRectCornerTopRight toView:self.containerView];
    [self addSubview:self.maskView];
    [self addSubview:self.containerView];
    // 添加表格
    [self.containerView addSubview:self.pickerV];
    self.pickerV.frame = CGRectMake(0, 80, self.containerView.width, kCustomPickerCellHeight*7); // 7行cell的高度
    // 表格的中间添加两个横线
    UIImageView *lineImgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.pickerV.top + kCustomPickerCellHeight*3, self.containerView.width, 0.5)];
    lineImgV1.backgroundColor = [UIColor lightGrayColor];
    [self.containerView addSubview:lineImgV1];
    UIImageView *lineImgV2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.pickerV.top + kCustomPickerCellHeight*4, self.containerView.width, 0.5)];
    lineImgV2.backgroundColor = [UIColor lightGrayColor];
    [self.containerView addSubview:lineImgV2];
    
    _topBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    [self.containerView addSubview:self.topBarView];
    [_topBarView addSubview:self.doneBtn];
    [_topBarView addSubview:self.cancelBtn];
    [_topBarView addSubview:self.titleLbl];
    _topBarView.backgroundColor = [UIColor whiteColor];
        
    self.doneBtn.frame = CGRectMake(self.frame.size.width - 44 - 15, 0, 44, 50);
    self.cancelBtn.frame = CGRectMake(15, 0, 44, 50);
    self.titleLbl.frame = CGRectMake(50, 0, self.frame.size.width - 100, 50);
}

#pragma mark - 弹出展示
/// 弹出展示
- (void)showAction {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [UIView animateWithDuration:0.25
                     animations:^{
        self.containerView.frame = CGRectMake(0, kScreenHeight_XM - self.contentHeight, kScreenWidth_XM ,  self.contentHeight);
        self.maskView.alpha = 0.6;
    }];
    
    [self.pickerV reloadAllComponents];
    // 去除自带样式
    for (UIView *subV in self.pickerV.subviews) {
        subV.backgroundColor = [UIColor clearColor];
    }
}

#pragma mark - 表格代理

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count; // 应该是二维数组
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.backgroundColor = [UIColor clearColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    if (component == 0) {
        NSString *titleStr = self.dataArray[row];
        titleLbl.text = titleStr;
    } else {
        NSString *titleStr = self.dataArray2[row];
        titleLbl.text = titleStr;
    }

    // 设置样式
    if ([pickerView selectedRowInComponent:component] == row) {
        titleLbl.font = [UIFont boldSystemFontOfSize:16];
    } else {
        titleLbl.font = [UIFont systemFontOfSize:16];
    }
    return titleLbl;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self.pickerV reloadAllComponents];
//    [pickerView selectedRowInComponent:0]
//    [pickerView selectedRowInComponent:0]
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return kScreenWidth_XM/2.0;
    } else {
        return kScreenWidth_XM/2.0;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return  kCustomPickerCellHeight;
}

#pragma mark - 隐藏
- (void)hideAction {
    [UIView animateWithDuration:0.22
                     animations:^{
        self.containerView.frame = CGRectMake(0, self.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height);
        self.maskView.alpha = 0.0;
    }
                     completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 确定按钮点击
- (void)doneAction {
    [self hideAction];
    if (self.submitBlock) {
        self.submitBlock(self.currentRow);
    }
}

#pragma mark - 取消按钮点击
- (void)cancelAction {
    [self hideAction];
}

#pragma mark - 懒加载
- (UIButton *)doneBtn {
    if (!_doneBtn) {
        _doneBtn = [[UIButton alloc] init];
        [_doneBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor redColor]  forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.textColor = [UIColor blackColor];
        _titleLbl.font = [UIFont systemFontOfSize:18];
        _titleLbl.textAlignment = NSTextAlignmentCenter;
        _titleLbl.text = @"日期选择";
    }
    return _titleLbl;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth_XM, kScreenHeight_XM)];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0.0;
    }
    return _maskView;
}

- (UIPickerView *)pickerV {
    if (!_pickerV) {
        _pickerV = [[UIPickerView alloc] init];
        _pickerV.backgroundColor = [UIColor whiteColor];
        _pickerV.delegate = self;
        _pickerV.dataSource = self;
    }
    return _pickerV;
}

#pragma mark - 为了不依赖其他类,把切圆角的方法,写到这里
/**
 *  添加UIView的四个角的任意角的圆角 需要对设置圆角的view添加frame,否则不行
 *
 *  @param angle    圆角的大小
 *  @param corners  设置哪个角为圆角
 *  @param toView  给哪个view设置圆角
 */
- (void)addRectCorner:(float)angle corners:(UIRectCorner)corners toView:(UIView *)toView {
    // 设置圆角,需要view有frame
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:toView.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(angle, angle)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = toView.bounds;
    maskLayer.path = maskPath.CGPath;
    toView.layer.mask = maskLayer;
}


@end


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

推荐阅读更多精彩内容

  • 系统提供的UIDatePickerView或者UIPickerView,默认会有曲面的3d效果想一个圆柱一样如下图...
    liang1991阅读 1,757评论 12 4
  • 根据需求可以自定义为自己的格式,这份demo的需求是:picker上面添加包含取消和确定的按钮视图。 具体如图: ...
    Miss_QL阅读 2,210评论 2 1
  • 效果图如上: 1.h文件声明 2.m文件实现 3.在外界添加这个View,并且回调block
    船长_阅读 3,884评论 1 7
  • 今天写了一个自定义的pickerview,是关于时间选择的,先上图,有兴趣的朋友可以看看. 先创建一个model....
    徐老茂阅读 4,021评论 0 13
  • 分享一个自定义的日期选择控件,或者其他自定义选择项,通过UIPickerView实现,实现Pickerview的几...
    T_aa阅读 6,817评论 4 10