|
本帖最后由 Koson 于 2013-7-18 17:36 编辑
由于时间关系,今天简单的分享一下横向UIPickerView的实现。后续再分享[IOS左右滑动菜单]、[kal Calendar日历使用]、[异步加在图片三方类库的使用]、[Facebook官方API的使用]等其他帖子。
1、UIPickerView在iPhone中使用频率非常之高,但大多数都是竖向的。刚好项目需要用到横向选择器,所以网上找找,还是有很多三方的库。今天分享的是自己代码实现的方式。
2、新建自己的ViewController,并且实现委托,.h文件:- @interface SearchViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
复制代码- UIPickerView *disPicker;
- NSMutableArray *itemDis;
复制代码 3、.m文件viewDidLoad中如下:- CGAffineTransform rotateItem = CGAffineTransformMakeRotation(3.14/2);
- rotateItem = CGAffineTransformScale(rotateItem, 1, 10);
-
- UIFont *myFont = [UIFont boldSystemFontOfSize:14];
-
- itemDis = [[NSMutableArray alloc] init];
- for (int i = 0; i < [listDISSNM count]; i++) {
- District *district = [listDISSNM objectAtIndex:i];
- UILabel *labItem = [[UILabel alloc] init];
- labItem.text = district.name;
- labItem.frame = CGRectMake(0, 0, 70, 100);
- labItem.backgroundColor = [UIColor clearColor];
- labItem.shadowColor = [UIColor whiteColor];
- labItem.shadowOffset = CGSizeMake(-1,-1);
- labItem.adjustsFontSizeToFitWidth = YES;
- [labItem setFont:myFont];
- [labItem setTextAlignment:UITextAlignmentCenter];
- [labItem setTextColor:[UIColor colorWithRed:0.604 green:0.604 blue:0.604 alpha:1]];
- labItem.transform = rotateItem;
- [itemDis addObject:labItem];
- }
复制代码- CGAffineTransform rotate = CGAffineTransformMakeRotation(-3.14/2);
- rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
-
- disPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, margin * 2, 320, 320)];
- [disPicker setTag:TAG_PICKER_DIS];
- disPicker.delegate = self;
- disPicker.dataSource= self;
- disPicker.showsSelectionIndicator =NO;
- [disPicker setBackgroundColor:[UIColor clearColor]];
- [self.view addSubview:disPicker];
- [disPicker setTransform:rotate];
- disPicker.center = CGPointMake(205,margin * 2 + 20);
- UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_02.png"]];
- [image setFrame:CGRectMake(disPicker.frame.origin.x - 10, disPicker.frame.origin.y, disPicker.frame.size.width + 20, disPicker.frame.size.height)];
- [self.view insertSubview:image belowSubview:disPicker];
复制代码 4、主要是下面的委托方法:- - (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- {
- [(UIView*)[[thePickerView subviews] objectAtIndex:0] setHidden:YES];
- [(UIView*)[[thePickerView subviews] objectAtIndex:4] setHidden:YES];
- [(UIView*)[[thePickerView subviews] objectAtIndex:1] setHidden:YES];
- [(UIView*)[[thePickerView subviews] objectAtIndex:3] setHidden:YES];
-
- switch ([thePickerView tag]) {
- case TAG_PICKER_DIS:
- return (UILabel *)[itemDis objectAtIndex:row];
- break;
- }
- return nil;
- }
复制代码 5、横向的UIPickerView就实现了,具体的以后再讨论。效果图如下:
|
|