工具类:自定义 pickerView

@property (weak, nonatomic) IBOutlet UIButton *constellationBtn;
@property (nonatomic,strong) UIView *pickerMainView;
@property (nonatomic,strong)UIPickerView *pickerView;//自定义pickerview
@property (nonatomic,strong)NSArray *letter;//保存要展示的字母  
//创建 自定义 pickerView
- (void)setMyPickerView {
    //设置数据
    self.letter = @[@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座",@"水瓶座",@"双鱼座"];
    // 初始化pickerView
    self.pickerMainView = [[UIView alloc]initWithFrame:CGRectMake(0, S_H, S_W, S_H)];
    [self.view addSubview:self.pickerMainView];
    //透明 button
    UIButton *backgroundBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, S_W, S_H - 190)];
    backgroundBtn.backgroundColor = [UIColor clearColor];
    [backgroundBtn addTarget:self action:@selector(clickCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerMainView addSubview:backgroundBtn];
    //下方view
    UIView *buttomView = [[UIView alloc]initWithFrame:CGRectMake(0, S_H - 190, S_W, 190)];
    buttomView.backgroundColor = [UIColor clearColor];
    [self.pickerMainView addSubview:buttomView];
    UIImageView *backGround = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, buttomView.width, buttomView.height)];
    backGround.image = [UIImage imageNamed:@"bg2"];
    [buttomView addSubview:backGround];
    //取消按钮
    UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 40, 40)];
    [cancelBtn setTitle:@"取消" forState: UIWindowLevelNormal];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIWindowLevelNormal];
    [cancelBtn addTarget:self action:@selector(clickCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [buttomView addSubview:cancelBtn];
    //确定按钮
    UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(buttomView.width - 56, 0, 40, 40)];
    [sureBtn setTitle:@"确定" forState: UIWindowLevelNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIWindowLevelNormal];
    [sureBtn addTarget:self action:@selector(clickSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [buttomView addSubview:sureBtn];
    //pickerView
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, buttomView.height - 150, S_W, 150)];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.pickerView.backgroundColor = [UIColor clearColor];
    [buttomView addSubview:self.pickerView];
    //指定数据源和委托
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    //跳到当前星座
    for (int i = 0; i < self.letter.count; i++) {
        NSString *tempStr = self.letter[i];
        if ([tempStr isEqualToString:self.constellationBtn.titleLabel.text]) {
            [self.pickerView selectRow:i inComponent:0 animated:YES];
            break;
        }
    }
}
//点击取消按钮
- (void)clickCancelBtn:(UIButton *)button {
    [UIView animateWithDuration:0.25 animations:^{
        self.pickerMainView.y = S_H;
    }completion:^(BOOL finished) {
        [self.pickerMainView removeFromSuperview];
        self.constellationBtn.enabled = YES;
    }];
}
//点击确定按钮
- (void)clickSureBtn:(UIButton *)button {
    [self.constellationBtn setTitle:self.constellationStr forState:UIControlStateNormal];
    [UIView animateWithDuration:0.25 animations:^{
        self.pickerMainView.y = S_H;
    }completion:^(BOOL finished) {
        [self.pickerMainView removeFromSuperview];
        self.constellationBtn.enabled = YES;
    }];
}

#pragma mark - UIPickerViewDataSource & UIPickerViewDelegate

//指定pickerview有几个表盘
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
//指定每个表盘上有几行数据
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.letter.count;//根据数组的元素个数返回几行数据
}
//设置内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.letter[row];
}
//设置字体颜色
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        //        pickerLabel.adjustsFontSizeToFitWidth = YES;
        //        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        pickerLabel.textColor = [UIColor whiteColor];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        //        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text = self.letter[row];
    return pickerLabel;
}
//取出选中的那个
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.constellationStr = self.letter[row];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值