iOS-OC版的多功能时间选择器

项目需要自己简单封装了一款多功能的时间选择器、欢迎大家使用。如果有更好的建议、不吝赐教,谢谢!

image.png

使用方法

    SPDateTimePickerView *pickerView = [[SPDateTimePickerView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
    pickerView.pickerViewMode = SPDatePickerModeDateHourMinuteSecond;
    pickerView.delegate = self;
    pickerView.title = @"时间选择器";
    [self.view addSubview:pickerView];
    [pickerView showDateTimePickerView];

使用例子:


image.png

代码:

#import "ViewController.h"
#import "SPDateTimePickerView.h"

@interface ViewController ()<SPDateTimePickerViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = @[@"年",
                       @"年月",
                       @"年月日",
                       @"年月日时",
                       @"年月日时分",
                       @"年月日时分秒",
                       @"时分",
                       @"时分秒",
                       @"分秒"];
    
    //方块的个数
    NSInteger count = array.count;
    
    //方块的尺寸
    NSInteger maxColsCount = 2;//一行中最大列数
    CGFloat buttonW = [UIScreen mainScreen].bounds.size.width/maxColsCount;
    CGFloat buttonH = 80;
    
    //创建所有的方块
    for (int i = 0; i < count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];

        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat buttonX = (i % maxColsCount) * buttonW;//button的X
        CGFloat buttonY = 100 + (i / maxColsCount) * buttonH;//button的Y
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
    }
}
- (void)buttonOnClick:(UIButton *)btn{
    SPDateTimePickerView *pickerView = [[SPDateTimePickerView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
    pickerView.pickerViewMode = btn.tag - 100;
    pickerView.delegate = self;
    pickerView.title = @"时间选择器";
    [self.view addSubview:pickerView];
    [pickerView showDateTimePickerView];
}
#pragma mark - SPDateTimePickerViewDelegate
- (void)didClickFinishDateTimePickerView:(NSString *)date {
    NSLog(@"%@",date);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

如有需要点击下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容