iOS 自定义日历

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

iOS自定义日历大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。其他就是展示和一些逻辑判断。下面贴上代码:ViewController:[objc]viewplaincopy在CODE上查看代码片派生到我的代码片#importUIKit/UIKit.h@interfaceViewController:UIViewController@end/***---------------分割线---------------***/#importViewController.h#importHWCalendar.h@interfaceViewController()UITextFieldDelegate,HWCalendarDelegate@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;}@endHWCalendar:[objc]viewplaincopy在CODE上查看代码片派生到我的代码片#importUIKit/UIKit.h@classHWCalendar;@protocolHWCalendarDelegateNSObject-(void)calendar:(HWCalendar*)calendardidClickSureButtonWithDate:(NSString*)date;@end@interfaceHWCalendar:UIView@property(nonatomic,assign)BOOLshowTimePicker;//defaultisNO.doesn'tshowtimePicker@property(nonatomic,weak)idHWCalendarDelegatedelegate;-(void)show;-(void)dismiss;@end/***---------------分割线---------------***/#importHWCalendar.h#importHWOptionButton.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()UIPickerViewDelegate,UIPickerViewDataSource,HWOptionButtonDelegate@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;iKShowYearsCount;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.textAlignmen

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功