/// 选择的回调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