标签:
首先 想应用PickerView 要引入两个代理
UIPickerViewDelegate,UIPickerViewDataSource
然后 我们在.m里面建立 UIPickerView
由于默认pickerView 只有一个滚轮 所以 想要自定义PickerView 是在一个UIView上面添加 pickerView 放在下面 然后在View的其他地方放Button什么之类的
UIView *pickerView=[[UIView alloc]initWithFrame:CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 226.0*SCREEN_HEIGHT/568)];
pickerView.backgroundColor=selfGrayColor;
self.pickerBackView =pickerView;
//添加View
UIButton *finshButton=[[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-60, 0.0, 50.0, 40)];
[finshButton setTitle:@"完成" forState:UIControlStateNormal];
[finshButton setTitleColor:selfBlueColor forState:UIControlStateNormal];
finshButton.layer.cornerRadius=10;
[finshButton addTarget:self action:@selector(tapActionClick1:) forControlEvents:UIControlEventTouchUpInside];
[self.pickerBackView addSubview:finshButton];
UIButton *disButton=[[UIButton alloc]initWithFrame:CGRectMake(10.0, 0.0, 50.0, 40)];
[disButton setTitle:@"取消" forState:UIControlStateNormal];
[disButton setTitleColor:selfBlueColor forState:UIControlStateNormal];
disButton.layer.cornerRadius=10;
[disButton addTarget:self action:@selector(tapActionClick1:) forControlEvents:UIControlEventTouchUpInside];
[self.pickerBackView addSubview:disButton];
//View上添加Button
self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0,40.0, [UIScreen mainScreen].bounds.size.width, self.pickerBackView.frame.size.height-60)];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.backgroundColor = [UIColor whiteColor];
[self.pickerBackView addSubview:self.pickerView];//pickerView添加到View上
[self.view addSubview:self.pickerBackView];
然后是pickerView的代理 跟TableView的代理很像 基本都一样
//多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;//即有两个滚轮
}
//多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//component 可以看成tableView的section
return 10;//每个滚轮有10行 如果不一样的行数 判断component 值 分别return就OK了
}
//自定义 每一个行样子
- (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 setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont systemFontOfSize:13]];
}
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
//每行显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
{
可以看成cell的处理方法 但是 因为pickerView只有title所以 返回的只有字符串就OK了
return string;
}
//滑动触发的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{ //这个是每一次滑动都会触发这个方法 但注意 如果只是打开pickerView 不会调用 只有滑动了之后才会触发 所以如果要处理第一次的默认传值 要自己注意
}
//动画 放在要调用pickerView的地方就行了 会从西面自动弹出 根据需求不同 随意更改
//弹出pickerView的简单动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.pickerBackView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height-(256*SCREEN_HEIGHT/568), [UIScreen mainScreen].bounds.size.width, 256*SCREEN_HEIGHT/568);
[UIView commitAnimations];
[self.pickerView selectRow:0 inComponent:0 animated:NO];
//回收键盘
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.pickerBackView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 226*SCREEN_HEIGHT/568);
[UIView commitAnimations];
没时间了 有时间详写
标签:
原文地址:http://www.cnblogs.com/mtforjimmy/p/5524111.html