@property (weak, nonatomic) IBOutlet UIButton *constellationBtn;
@property (nonatomic,strong) UIView *pickerMainView;
@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,strong)NSArray *letter;
- (void)setMyPickerView {
self.letter = @[@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座",@"水瓶座",@"双鱼座"];
self.pickerMainView = [[UIView alloc]initWithFrame:CGRectMake(0, S_H, S_W, S_H)];
[self.view addSubview:self.pickerMainView];
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];
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];
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
- (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];
pickerLabel.textColor = [UIColor whiteColor];
pickerLabel.textAlignment = NSTextAlignmentCenter;
}
pickerLabel.text = self.letter[row];
return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.constellationStr = self.letter[row];
}