iOS 自定义日历.docx
《iOS 自定义日历.docx》由会员分享,可在线阅读,更多相关《iOS 自定义日历.docx(39页珍藏版)》请在冰豆网上搜索。
iOS自定义日历
iOS自定义日历
大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。
每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。
获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。
其他就是展示和一些逻辑判断。
下面贴上代码:
ViewController:
[objc]viewplaincopy在CODE上查看代码片派生到我的代码片
#import
@interfaceViewController:
UIViewController
@end
/***---------------分割线---------------***/
#import"ViewController.h"
#import"HWCalendar.h"
@interfaceViewController()
@property(nonatomic,weak)HWCalendar*calendar;
@property(nonatomic,strong)UITextField*textField;
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
self.view.backgroundColor=[UIColorblackColor];
//创建控件
[selfcreatControl];
}
-(void)creatControl
{
//输入框
_textField=[[UITextFieldalloc]initWithFrame:
CGRectMake(7,64,400,44)];
_textField.delegate=self;
_textField.layer.cornerRadius=22;
_textField.layer.masksToBounds=YES;
_textField.placeholder=@"请设置日期";
_textField.textAlignment=NSTextAlignmentCenter;
_textField.backgroundColor=[UIColorwhiteColor];
[self.viewaddSubview:
_textField];
//日历
HWCalendar*calendar=[[HWCalendaralloc]initWithFrame:
CGRectMake(7,[UIScreenmainScreen].bounds.size.height,400,396)];
calendar.delegate=self;
calendar.showTimePicker=YES;
[self.viewaddSubview:
calendar];
self.calendar=calendar;
}
#pragmamark-UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:
(UITextField*)textField
{
if(_calendar.frame.origin.y!
=[UIScreenmainScreen].bounds.size.height&&_calendar){
[_calendardismiss];
returnNO;
}elseif(textField==_textField){
[_calendarshow];
returnNO;
}
returnYES;
}
#pragmamark-HWCalendarDelegate
-(void)calendar:
(HWCalendar*)calendardidClickSureButtonWithDate:
(NSString*)date
{
_textField.text=date;
}
@end
HWCalendar:
[objc]viewplaincopy在CODE上查看代码片派生到我的代码片
#import
@classHWCalendar;
@protocolHWCalendarDelegate
-(void)calendar:
(HWCalendar*)calendardidClickSureButtonWithDate:
(NSString*)date;
@end
@interfaceHWCalendar:
UIView
@property(nonatomic,assign)BOOLshowTimePicker;//defaultisNO.doesn'tshowtimePicker
@property(nonatomic,weak)iddelegate;
-(void)show;
-(void)dismiss;
@end
/***---------------分割线---------------***/
#import"HWCalendar.h"
#import"HWOptionButton.h"
#defineKCol7
#defineKBtnW44
#defineKBtnH44
#defineKMaxCount37
#defineKBtnTag100
#defineKTipsW92
#defineKShowYearsCount100
#defineKMainColor[UIColorcolorWithRed:
0.0fgreen:
139/255.0fblue:
125/255.0falpha:
1.0f]
#defineKbackColor[UIColorcolorWithRed:
173/255.0fgreen:
212/255.0fblue:
208/255.0falpha:
1.0f]
@interfaceHWCalendar()
@property(nonatomic,strong)NSArray*weekArray;
@property(nonatomic,strong)NSArray*timeArray;
@property(nonatomic,strong)NSArray*yearArray;
@property(nonatomic,strong)NSArray*monthArray;
@property(nonatomic,strong)UIPickerView*timePicker;
@property(nonatomic,weak)UIView*calendarView;
@property(nonatomic,weak)HWOptionButton*yearBtn;
@property(nonatomic,weak)HWOptionButton*monthBtn;
@property(nonatomic,weak)UILabel*weekLabel;
@property(nonatomic,weak)UILabel*yearLabel;
@property(nonatomic,weak)UILabel*monthLabel;
@property(nonatomic,weak)UILabel*dayLabel;
@property(nonatomic,assign)NSIntegeryear;
@property(nonatomic,assign)NSIntegermonth;
@property(nonatomic,assign)NSIntegerday;
@property(nonatomic,assign)NSIntegerhour;
@property(nonatomic,assign)NSIntegerminute;
@property(nonatomic,assign)NSIntegercurrentYear;
@property(nonatomic,assign)NSIntegercurrentMonth;
@property(nonatomic,assign)NSIntegercurrentDay;
@end
@implementationHWCalendar
-(instancetype)initWithFrame:
(CGRect)frame
{
if(self=[superinitWithFrame:
frame]){
//获取当前时间
[selfgetCurrentDate];
//获取数据源
[selfgetDataSource];
//创建控件
[selfcreatControl];
//初始化设置
[selfsetDefaultInfo];
//刷新数据
[selfreloadData];
}
returnself;
}
-(void)getDataSource
{
_weekArray=@[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
_timeArray=@[@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"]];
NSIntegerfirstYear=_year-KShowYearsCount/2;
NSMutableArray*yearArray=[NSMutableArrayarray];
for(inti=0;i[yearArrayaddObject:
[NSStringstringWithFormat:
@"%ld",firstYear+i]];
}
_yearArray=yearArray;
_monthArray=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
}
-(void)setDefaultInfo
{
self.backgroundColor=[UIColorwhiteColor];
[_timePickerselectRow:
_hourinComponent:
0animated:
NO];
[_timePickerselectRow:
_minute-1inComponent:
1animated:
NO];
_currentYear=_year;
_currentMonth=_month;
_currentDay=_day;
}
-(void)creatControl
{
//左侧显示视图
UIView*tipsView=[[UIViewalloc]initWithFrame:
CGRectMake(0,0,KTipsW,KBtnH*8)];
tipsView.backgroundColor=KMainColor;
[selfaddSubview:
tipsView];
//星期标签
UILabel*weekLabel=[[UILabelalloc]initWithFrame:
CGRectMake(0,0,KTipsW,KBtnH)];
weekLabel.backgroundColor=[UIColorcolorWithRed:
0.0fgreen:
130/255.0fblue:
116/255.0falpha:
1.0f];
weekLabel.textColor=[UIColorwhiteColor];
weekLabel.textAlignment=NSTextAlignmentCenter;
[tipsViewaddSubview:
weekLabel];
self.weekLabel=weekLabel;
//年份标签
UILabel*yearLabel=[[UILabelalloc]initWithFrame:
CGRectMake(0,CGRectGetMaxY(weekLabel.frame)+20,KTipsW,KBtnH)];
yearLabel.textColor=KbackColor;
yearLabel.textAlignment=NSTextAlignmentCenter;
yearLabel.font=[UIFontsystemFontOfSize:
26.0f];
[tipsViewaddSubview:
yearLabel];
self.yearLabel=yearLabel;
//月份标签
UILabel*monthLabel=[[UILabelalloc]initWithFrame:
CGRectMake(0,CGRectGetMaxY(yearLabel.frame),KTipsW,26)];
monthLabel.textColor=[UIColorwhiteColor];
monthLabel.textAlignment=NSTextAlignmentCenter;
monthLabel.font=[UIFontsystemFontOfSize:
26.0f];
[tipsViewaddSubview:
monthLabel];
self.monthLabel=monthLabel;
//日期标签
UILabel*dayLabel=[[UILabelalloc]initWithFrame:
CGRectMake(0,CGRectGetMaxY(monthLabel.frame)+30,KTipsW,120)];
dayLabel.textColor=[UIColorwhiteColor];
dayLabel.textAlignment=NSTextAlignmentCenter;
dayLabel.font=[UIFontsystemFontOfSize:
76.0f];
[tipsViewaddSubview:
dayLabel];
self.dayLabel=dayLabel;
CGFloatyearBtnW=70.0f;
CGFloatmonthbtnW=60.0f;
CGFloattodayBtnW=70.0f;
CGFloatpadding=(self.bounds.size.width-KTipsW-yearBtnW-monthbtnW-todayBtnW-KBtnW*2)*0.25;
//年份按钮
HWOptionButton*yearBtn=[[HWOptionButtonalloc]initWithFrame:
CGRectMake(KTipsW+padding,0,yearBtnW,KBtnH)];
yearBtn.array=_yearArray;
yearBtn.row=KShowYearsCount/2;
yearBtn.delegate=self;
[selfaddSubview:
yearBtn];
self.yearBtn=yearBtn;
//上一月
UIButton*preBtn=[[UIButtonalloc]initWithFrame:
CGRectMake(CGRectGetMaxX(yearBtn.frame)+padding,0,KBtnW,KBtnH)];
[preBtnsetImage:
[UIImageimageNamed:
@"left"]forState:
UIControlStateNormal];
[preBtnaddTarget:
selfaction:
@selector(preBtnOnClick)forControlEvents:
UIControlEventTouchUpInside];
[selfaddSubview:
preBtn];
//月份按钮
HWOptionButton*monthBtn=[[HWOptionButtonalloc]initWithFrame:
CGRectMake(CGRectGetMaxX(preBtn.frame),0,monthbtnW,KBtnH)];
monthBtn.array=_monthArray;
monthBtn.row=_month-1;
monthBtn.delegate=self;
[selfaddSubview:
monthBtn];
self.monthBtn=monthBtn;
//下一月
UIButton*nextBtn=[[UIButtonalloc]initWithFrame:
CGRectMake(CGRectGetMaxX(monthBtn.frame),0,KBtnW,KBtnH)];
[nextBtnsetImage:
[UIImageimageNamed:
@"right"]forState:
UIControlStateNormal];
[nextBtnaddTarget:
selfaction:
@selector(nextBtnOnClick)forControlEvents:
UIControlEventTouchUpInside];
[selfaddSubview:
nextBtn];
//返回今天按钮
UIButton*backTodayBtn=[[UIButtonalloc]initWithFrame:
CGRectMake(CGRectGetMaxX(nextBtn.frame)+padding,0,todayBtnW,KBtnH)];
backTodayBtn.titleLabel.font=[UIFontsystemFontOfSize:
14.0f];
[backTodayBtnsetTitleColor:
[UIColorblackColor]forState:
UIControlStateNormal];
[backTodayBtnsetTitle:
@"返回今天"forState:
UIControlStateNormal];
[backTodayBtnaddTarget:
selfaction:
@selector(backTodayBtnOnClick)forControlEvents:
UIControlEventTouchUpInside];
[selfaddSubview:
backTodayBtn];
//星期标签
for(inti=0;i<_weekArray.count;i++){
UILabel*week=[[UILabelalloc]initWithFrame:
CGRectMake(KTipsW+KBtnH*i,KBtnH,KBtnH,KBtnH)];
week.textAlignment=NSTextAlignmentCenter;
week.text=_weekArray[i];
[selfaddSubview:
week];
}
//日历核心视图
UIView*calendarView=[[UIViewalloc]initWithFrame:
CGRectMake(KTipsW,KBtnH*2,KBtnW*7,KBtnH*6)];
[selfaddSubview:
calendarView];
self.calendarView=calendarView;
//每一个日期用一个按钮去创建,当一个月的第一天是星期六并且有31天时为最多个数,5行零2个,共37个
for(i