iOS开发(OC)——日期选择器

来源:互联网 发布:rxjava 源码 编辑:程序博客网 时间:2024/05/21 18:40

可以选择年月日时分
新建继承UIView的新类DatePickerView
DatePickerView.h代码

#import <UIKit/UIKit.h>typedef void(^Select)(int stag);//确定/取消@interface DatePickerView : UIView<UIScrollViewDelegate>@property (nonatomic,strong)UILabel *titleLabel;@property (nonatomic,strong)UIScrollView *scrollView;@property (nonatomic,strong)UIPageControl *pageControl;@property (nonatomic,strong)UIButton *cancelBut;@property (nonatomic,strong)UIButton *okBut;@property (nonatomic,copy)Select block;@property (nonatomic,strong)NSString *timestamp;@end

DatePickerView.m代码

import “DatePickerView.h”

@implementation DatePickerView

-(instancetype)initWithFrame:(CGRect)frame
{
if(self==[super initWithFrame:frame]){
self.backgroundColor=[UIColor whiteColor];
self.layer.cornerRadius=10.0;
self.layer.masksToBounds=YES;

    [self initTitleLabel];    [self initDatePicker];}return self;

}

-(void)initTitleLabel
{
self.titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.frame.size.width, 21)];
self.titleLabel.font=[UIFont systemFontOfSize:20];
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@”YYYY-MM-dd HH:mm”];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
self.titleLabel.text=dateString;
self.titleLabel.textColor=[UIColor redColor];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
[self addSubview:self.titleLabel];
}

-(void)initDatePicker
{
self.scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, 200)];
self.scrollView.pagingEnabled=YES;
self.scrollView.contentSize=CGSizeMake(self.frame.size.width*2, 200);
self.scrollView.showsHorizontalScrollIndicator=NO;
self.scrollView.delegate=self;
[self addSubview:self.scrollView];

for(int i=0;i<2;i++){    UIDatePicker *picker=[[UIDatePicker alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width*i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];    if(i==0){        picker.datePickerMode=UIDatePickerModeDate;//年月日    }else{        picker.datePickerMode=UIDatePickerModeTime;//时分    }    [picker setDate:[NSDate date]];    picker.tag=i;    [picker addTarget:self action:@selector(pickerChange:) forControlEvents:UIControlEventValueChanged];    [self.scrollView addSubview:picker];}self.pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 235, self.frame.size.width, 10)];self.pageControl.numberOfPages = 2;self.pageControl.currentPage = 0;self.pageControl.pageIndicatorTintColor=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];//页码点的颜色self.pageControl.currentPageIndicatorTintColor=[UIColor redColor];//当前页码点的颜色[self addSubview:self.pageControl];UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 255, self.frame.size.width, 1)];lineView.backgroundColor=[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1.0];[self addSubview:lineView];self.cancelBut=[UIButton buttonWithType:UIButtonTypeSystem];self.cancelBut.frame=CGRectMake(0, 259, self.frame.size.width/2, 40);[self.cancelBut setTitle:@"取消" forState:0];[self.cancelBut setTitleColor:[UIColor darkGrayColor] forState:0];[self.cancelBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];self.cancelBut.tag=0;self.cancelBut.titleLabel.font=[UIFont systemFontOfSize:18];[self addSubview:self.cancelBut];self.okBut=[UIButton buttonWithType:UIButtonTypeSystem];self.okBut.frame=CGRectMake(self.frame.size.width/2, 259, self.frame.size.width/2, 40);[self.okBut setTitle:@"确定" forState:0];[self.okBut setTitleColor:[UIColor darkGrayColor] forState:0];[self.okBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];self.okBut.tag=1;self.okBut.titleLabel.font=[UIFont systemFontOfSize:18];[self addSubview:self.okBut];

}

-(void)pickerChange:(UIDatePicker *)picker
{
switch (picker.tag) {
case 0:
{
NSDate *currentDate = picker.date;
self.timestamp=[NSString stringWithFormat:@”%ld”,(long)[currentDate timeIntervalSince1970]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@”YYYY-MM-dd”];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSString *initialString=self.titleLabel.text;
self.titleLabel.text=[NSString stringWithFormat:@”%@ %@”,dateString,[initialString substringWithRange:NSMakeRange(11, 5)]];
}
break;
case 1:
{
NSDate *currentDate = picker.date;
self.timestamp=[NSString stringWithFormat:@”%ld”,(long)[currentDate timeIntervalSince1970]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@”HH:mm”];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSString *initialString=self.titleLabel.text;
self.titleLabel.text=[NSString stringWithFormat:@”%@ %@”,[initialString substringToIndex:10],dateString];
}
break;

    default:        break;}

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.pageControl.currentPage=scrollView.contentOffset.x/self.frame.size.width;
}

-(void)buttonAction:(UIButton *)sender
{
self.block((int)sender.tag);
}

0 0
原创粉丝点击