iOS自定义pickerView

demo.gif

效果图如上:

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];          
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,025评论 25 708
  • 分享一个自定义的日期选择控件,或者其他自定义选择项,通过UIPickerView实现,实现Pickerview的几...
    T_aa阅读 6,855评论 4 10
  • 1,NSObject中description属性的意义,它可以重写吗?答案:每当 NSLog(@"")函数中出现 ...
    eightzg阅读 4,184评论 2 19
  • 感受到她发自内心的开心,三个月来,她没有主动跟我说过一句话,安静,待在角落里不吵也不闹,今天跟我讲了好多。一接触到...
    一颗山枣阅读 206评论 0 0
  • 写这篇文章时,心里充满着羞愧。因为自己对爸妈,远没有爸妈对我和弟弟那样不计得失,不记付出,且永远放在第一位。 这个...
    零七柠乐阅读 899评论 0 0