UIPickView

测试单列.png
测试双列.png
测试多列.png

AZPickiew是基于UIPickView做的封装,用法相对简单

@interface AZPickView : UIView
@property (nonatomic , strong) NSArray * dataSource;
@property (nonatomic , weak) id<AZPickViewDeleagte> delegate;
// PickView的宽,默认是60,如果界面仍然不能显示完全,适当增大。
@property (nonatomic , assign) float pickWidth;
// PickView的高,默认是35,如果不满足,可适当增大
@property (nonatomic ,assign) float rowHeight;
// // 设置初始化时所对应的数据,可为空。
@property (nonatomic ,strong) NSArray * pickInitArray;
// 加载页面
+ (instancetype) loadView;

用法:

#import "ViewController.h"
#import "AZPickView.h"
// 屏幕 SCREEN_WIDTH
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
//屏幕 SCREEN_HEIGHT
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
@interface ViewController ()<AZPickViewDeleagte>
@property (nonatomic ,strong) UIButton * currentBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initUI];
}
- (void) initUI{
    for (int i = 0; i < 3; i ++) {
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 70 + i *150, SCREEN_WIDTH -100, 21)];
        [label setText:[NSString stringWithFormat:@"列数为%d的pickView",i+1]];
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(50, 100 + i *150, SCREEN_WIDTH -100, 44)];
        button.tag = i;
        NSString *btnTitle ;
        switch (i) {
            case 0:
                btnTitle = @"关闭";
                break;
                case 1:
                btnTitle = @"12:00";
                break;
            default:
                btnTitle = @"2017年-04月-19日 09时:59分";
                break;
        }
        [button setTitle:btnTitle forState:UIControlStateNormal];
        button.backgroundColor = [UIColor orangeColor];
        [button addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:label];
        [self.view addSubview:button];
    }
}
- (void) btnOnclick:(UIButton *)btn{
    switch (btn.tag) {
        case 0:
            [self test0:btn];
            break;
            case 1:
            [self test1:btn];
            break;
        default:
            [self test2:btn];
            break;
    }
}
- (void) test0:(UIButton *)btn{
    self.currentBtn = btn;
    AZPickView * pickView = [AZPickView loadView];
    pickView.pickWidth = 90;
    [pickView setFrame:CGRectMake(0, SCREEN_HEIGHT -250, SCREEN_WIDTH, 250)];
    pickView.delegate = (id)self;
    NSMutableArray * array = [NSMutableArray array];
    for (int i = 0; i < 360; i++) {
        NSString * str = [NSString stringWithFormat:@"%d分钟",i];
        [array addObject:str];
    }
    [array insertObject:@"关闭" atIndex:0];
    pickView.dataSource = @[array];
    pickView.pickInitArray  =  @[btn.currentTitle];
    [self.view addSubview:pickView];
  
}
- (void)test2:(UIButton *)btn2{
    self.currentBtn = btn2;
    AZPickView * pickView = [AZPickView loadView];
    pickView.pickWidth = 80;
    [pickView setFrame:CGRectMake(0, SCREEN_HEIGHT -250, SCREEN_WIDTH, 250)];
    pickView.delegate = (id)self;
    NSMutableArray * arrayYear = [NSMutableArray array];
    NSMutableArray * arrayMonth = [NSMutableArray array];
    NSMutableArray * arrayDay = [NSMutableArray array];
    NSMutableArray * arrayHour = [NSMutableArray array];
    NSMutableArray * arrayMin = [NSMutableArray array];
    for (int i = 2000; i < 2100; i++) {
        NSString * str = [NSString stringWithFormat:@"%d年",i];
        [arrayYear addObject:str];
    }
    for (int i = 1; i < 13; i++) {
        NSString * str = [NSString stringWithFormat:@"%.2d月",i];
        [arrayMonth addObject:str];
    }
    for (int i = 1; i < 31; i++) {
        NSString * str = [NSString stringWithFormat:@"%.2d日",i];
        [arrayDay addObject:str];
    }
    for (int i = 0; i <= 24; i++) {
        NSString * str = [NSString stringWithFormat:@"%.2d时",i];
        [arrayHour addObject:str];
    }
    for (int i = 0; i <= 59; i++) {
        NSString * str = [NSString stringWithFormat:@"%.2d分",i];
        [arrayMin addObject:str];
    }
    pickView.dataSource = @[arrayYear,arrayMonth,arrayDay,arrayHour,arrayMin];
    NSString * str = btn2.currentTitle;
    NSArray * strArray = [str componentsSeparatedByString:@" "];
    NSString * yyMMdd = strArray[0];
    NSString * HHmm = strArray[1];
    NSArray * yyMMddA = [yyMMdd componentsSeparatedByString:@"-"];
    NSArray * HHmmA = [HHmm componentsSeparatedByString:@":"];
    pickView.pickInitArray  =  @[yyMMddA[0],yyMMddA[1],yyMMddA[2],HHmmA[0],HHmmA[1]];
    [self.view addSubview:pickView];

}
- (void)test1:(UIButton *)btn1{
    self.currentBtn = btn1;
    AZPickView * pickView = [AZPickView loadView];
    [pickView setFrame:CGRectMake(0, SCREEN_HEIGHT -250, SCREEN_WIDTH, 250)];
    pickView.delegate = (id)self;
    NSMutableArray * arrayHour = [NSMutableArray array];
    NSMutableArray * arrayMin = [NSMutableArray array];
    
    for (int i = 0; i < 24; i++) {
        NSString * str = [NSString stringWithFormat:@"%.2d",i];
        [arrayHour addObject:str];
    }
    for (int i = 0 ; i<= 59; i ++) {
        NSString * str = [NSString stringWithFormat:@"%.2d",i];
        [arrayMin addObject:str];
    }
    pickView.dataSource = @[arrayHour,arrayMin];
    NSArray * array = [btn1.titleLabel.text componentsSeparatedByString:@":"];
    
    pickView.pickInitArray =  @[array[0],array[1]];;
    [self.view addSubview:pickView];

}
#pragma mark --- AZPPickViewDelegate
- (void)AZPickResoultWithArray:(NSArray *)array{
    switch (self.currentBtn.tag) {
        case 0:
            [self.currentBtn setTitle:array[0] forState:UIControlStateNormal];
            break;
         case 1:
            [self resolutionTest1:array];
            break;
        default:
            [self resolutionTest2:array];
            break;
    }
}
- (void)resolutionTest1:(NSArray *)array{
    NSString * s = [array componentsJoinedByString:@":"];
    [self.currentBtn setTitle:s forState:UIControlStateNormal];
}
- (void)resolutionTest2:(NSArray *)array{
    NSString * s = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",array[0],array[1],array[2],array[3],array[4]];
    [self.currentBtn setTitle:s forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

github地址:https://github.com/anzipeng/AZPickViewDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,828评论 6 526
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,669评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,467评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,238评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,039评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,561评论 1 319
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,658评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,806评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,316评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,293评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,443评论 1 364
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,998评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,698评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,097评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,333评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,012评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,434评论 2 370

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,554评论 25 707
  • 坚持原创分享第66天 小时候,中秋是长长的期待。 至少在八月初七八,家里就要买月饼了,是散装的那种五仁月饼。先用黄...
    宛如初夏阅读 253评论 0 0
  • 职场-长网址缩短为百度短网址 1.有些长网址不好看并且后面会失效,所以可以使用百度短网址,体验会好很多2.一般提加...
    rocqd阅读 201评论 0 0