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);
}
- iOS开发(OC)——日期选择器
- 《Android开发卷——自定义日期选择器(一)》
- 《Android开发卷——自定义日期选择器(二)》
- 《Android开发卷——自定义日期选择器(三)》
- iOS开发(OC)——录音
- iOS开发(OC)——UIWindow
- iOS日期选择器(UIDatePicker)
- ios开发 自定义日期选择器 PTXDatePickerView
- iOS 自定义UIDatePicker日期选择器视图 —— HERO博客
- iOS开发(Swift)——图片选择器
- iOS开发(OC)——文字滚动效果
- iOS开发(OC)——FMDB数据库的使用
- iOS开发(OC)——AFNetworking之文件上传
- iOS开发(OC)——AFNetworking之文件下载
- iOS开发(OC)——分段按钮的实现
- iOS开发(OC)——md5的加密
- iOS开发(OC)——根据url播放音频
- iOS开发(OC)——网络状态的检测
- JAVA-WEB开发第三讲[2016-06-04]JAVA环境变量的设置
- Android实训案例(九)——答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程
- CentOS安装openvpn
- 电力电子仿真软件---PLECS
- C语言calloc()函数
- iOS开发(OC)——日期选择器
- 动态库的调试和崩溃的处理方法
- python-day0001--环境搭建
- Ubuntu15.10 下安装Oracle 11g
- uva 1312 Cricket Field
- 随笔
- pandas的iloc的神奇之处
- win 10 某个WiFi无Internet 的决绝方案
- hdu 2616 dfs回溯暴力