效果图如上:
1.h文件声明
#import <UIKit/UIKit.h>
@interface DXPickerView : UIView
+ (instancetype)pickerViewWithBlock:(void(^)(NSInteger a))ret andSourceTitle:(NSArray*)array;
- (void)remove;
-(void)show;
@property(nonatomic,copy)void(^retBlock)(NSInteger index);
@property (nonatomic,strong)NSArray*arrayDataSource;
-(void)showWithIndex:(NSInteger)index;
@end
2.m文件实现
@interface DXPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic,assign) NSInteger pickerIndex;
@property (weak, nonatomic) IBOutlet UIView *viewBG;
@property (nonatomic,weak) UIButton *coverBtn;
@end
@implementation DXPickerView
- (void)awakeFromNib
{
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.frame =CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverBtnClick)];
[self addGestureRecognizer:tap];
[self setUserInteractionEnabled:YES];
[self.pickerView setBackgroundColor:[UIColor whiteColor]];
[self sendSubviewToBack:self.viewBG];
}
- (void)coverBtnClick
{
[self hide];
}
- (void)remove
{
[self hide];
}
// block先保存起来,在点击确定的时候回调
+ (instancetype)pickerViewWithBlock:(void(^)(NSInteger a))ret andSourceTitle:(NSArray*)array
{
NSBundle *bundle = [NSBundle mainBundle];
DXPickerView*picker=[[bundle loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
picker.retBlock=ret;
picker.arrayDataSource=array;
return picker;
}
- (IBAction)canceBtnClick:(id)sender {
[self remove];
}
- (IBAction)sureBtnClick:(id)sender {
NSInteger row=self.pickerIndex;
if (self.retBlock) {
self.retBlock(row);
}
[self remove];
}
// 返回有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// 返回第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.arrayDataSource.count;
}
//返回第component列高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 44;
}
// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.arrayDataSource[row];
}
// 监听UIPickerView选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.pickerIndex = row;
}
-(void)show
{
[self showWithIndex:0];
}
-(void)hide
{
[UIView animateWithDuration:0.4 animations:^{
self.frame =CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
-(void)showWithIndex:(NSInteger)index
{
if (index<self.arrayDataSource.count) {
[self.pickerView selectRow:index inComponent:0 animated:YES];
self.pickerIndex=index;
}
[[UIApplication sharedApplication].keyWindow addSubview:self];
[UIView animateWithDuration:0.4 animations:^{
self.frame =CGRectMake(0, 0, ScreenWidth, ScreenHeight);
}];
}
3.在外界添加这个View,并且回调block
@weakify(self);
DXPickerView *pickerView = [DXPickerView pickerViewWithBlock:^(NSInteger a) {
{
@strongify(self);
self.gender=[@(a+1)stringValue];
self.userModel.sex=[@(a+1)stringValue];
DataItsaidCell*nickCell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
if (nickCell) {
nickCell.NiCheng.text=[self generateNickName];
}
[self.tableView reloadData];
}
} andSourceTitle:@[@"男",@"女"]];
[pickerView show];
网友评论