工具类:自定义 pickerView

原创 2016年08月30日 22:28:23
@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];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

AsyncTask解析(下)——实现自定义AsyncTask网络传输工具类封装

在上文:AsyncTask深度解析(上)——原理分析与超简单demo实现中已经对AsyncTask的实现原理以及源码流程进行了一定的分析,相信大家已经基本熟悉了AsyncTask内部实现网络传输的调用...

java自定义服务器检验工具类

  • 2016年01月17日 01:04
  • 20KB
  • 下载

工具类——自定义Collections集合方法

项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式。 话不多说,看代码:1、封装的自定义集合工具类:CollectionsCustompackage com....

自己写的一些自定义View工具类

  • 2016年08月11日 09:34
  • 20.31MB
  • 下载

java基础编程,用户自定义替换list列表部分位置元素(基于collections工具类扩展)

collections工具类定义了一个方法fill(List
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:工具类:自定义 pickerView
举报原因:
原因补充:

(最多只允许输入30个字)