iOS 自定义日历_第1页
iOS 自定义日历_第2页
iOS 自定义日历_第3页
iOS 自定义日历_第4页
iOS 自定义日历_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、iOS 自定义日历大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。其他就是展示和一些逻辑判断。下面贴上代码:ViewController:objc view plain copy 在CODE上查看代码片派生到我的代码片#import interface ViewController : UIViewControl

2、ler end /* -分割线- */ #import ViewController.h #import HWCalendar.h interface ViewController () property (nonatomic, weak) HWCalendar *calendar; property (nonatomic, strong) UITextField *textField; end implementation ViewController - (void)viewDidLoad super viewDidLoad; self.view.backgroundColor = UIC

3、olor blackColor; /创建控件 self creatControl; - (void)creatControl /输入框 _textField = UITextField alloc initWithFrame:CGRectMake(7, 64, 400, 44); _textField.delegate = self; _textField.layer.cornerRadius = 22; _textField.layer.masksToBounds = YES; _textField.placeholder = 请设置日期; _textField.textAlignment

4、= NSTextAlignmentCenter; _textField.backgroundColor = UIColor whiteColor; self.view addSubview:_textField; /日历 HWCalendar *calendar = HWCalendar alloc initWithFrame:CGRectMake(7, UIScreen mainScreen.bounds.size.height, 400, 396); calendar.delegate = self; calendar.showTimePicker = YES; self.view add

5、Subview:calendar; self.calendar = calendar; #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField if (_calendar.frame.origin.y != UIScreen mainScreen.bounds.size.height & _calendar) _calendar dismiss; return NO; else if (textField = _textField) _calendar sho

6、w; return NO; return YES; #pragma mark - HWCalendarDelegate - (void)calendar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date _textField.text = date; end HWCalendar:objc view plain copy 在CODE上查看代码片派生到我的代码片#import class HWCalendar; protocol HWCalendarDelegate - (void)calendar:(HWCal

7、endar *)calendar didClickSureButtonWithDate:(NSString *)date; end interface HWCalendar : UIView property (nonatomic, assign) BOOL showTimePicker; /default is NO. doesnt show timePicker property (nonatomic, weak) id delegate; - (void)show; - (void)dismiss; end /* -分割线- */ #import HWCalendar.h #import

8、 HWOptionButton.h #define KCol 7 #define KBtnW 44 #define KBtnH 44 #define KMaxCount 37 #define KBtnTag 100 #define KTipsW 92 #define KShowYearsCount 100 #define KMainColor UIColor colorWithRed:0.0f green:139/255.0f blue:125/255.0f alpha:1.0f #define KbackColor UIColor colorWithRed:173/255.0f green:

9、212/255.0f blue:208/255.0f alpha:1.0f interface HWCalendar () property (nonatomic, strong) NSArray *weekArray; property (nonatomic, strong) NSArray *timeArray; property (nonatomic, strong) NSArray *yearArray; property (nonatomic, strong) NSArray *monthArray; property (nonatomic, strong) UIPickerView

10、 *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 *mo

11、nthLabel; property (nonatomic, weak) UILabel *dayLabel; property (nonatomic, assign) NSInteger year; property (nonatomic, assign) NSInteger month; property (nonatomic, assign) NSInteger day; property (nonatomic, assign) NSInteger hour; property (nonatomic, assign) NSInteger minute; property (nonatom

12、ic, assign) NSInteger currentYear; property (nonatomic, assign) NSInteger currentMonth; property (nonatomic, assign) NSInteger currentDay; end implementation HWCalendar - (instancetype)initWithFrame:(CGRect)frame if (self = super initWithFrame:frame) /获取当前时间 self getCurrentDate; /获取数据源 self getDataS

13、ource; /创建控件 self creatControl; /初始化设置 self setDefaultInfo; /刷新数据 self reloadData; return self; - (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,

14、 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; NSInteger firstYear = _year - KShowYearsCount / 2; NSMutableArray *yearArray = NSMutableArray array; fo

15、r (int i = 0; i KShowYearsCount; i+) yearArray addObject:NSString stringWithFormat:%ld, firstYear + i; _yearArray = yearArray; _monthArray = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; - (void)setDefaultInfo self.backgroundColor = UIColor whiteColor; _timePicker selectRow:_hour inComponent:0 animated:NO;

16、 _timePicker selectRow:_minute - 1 inComponent:1 animated:NO; _currentYear = _year; _currentMonth = _month; _currentDay = _day; - (void)creatControl /左侧显示视图 UIView *tipsView = UIView alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH * 8); tipsView.backgroundColor = KMainColor; self addSubview:tipsV

17、iew; /星期标签 UILabel *weekLabel = UILabel alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH); weekLabel.backgroundColor = UIColor colorWithRed:0.0f green:130/255.0f blue:116/255.0f alpha:1.0f; weekLabel.textColor = UIColor whiteColor; weekLabel.textAlignment = NSTextAlignmentCenter; tipsView addSubvi

18、ew:weekLabel; self.weekLabel = weekLabel; /年份标签 UILabel *yearLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(weekLabel.frame) + 20, KTipsW, KBtnH); yearLabel.textColor = KbackColor; yearLabel.textAlignment = NSTextAlignmentCenter; yearLabel.font = UIFont systemFontOfSize:26.0f; tipsV

19、iew addSubview:yearLabel; self.yearLabel = yearLabel; /月份标签 UILabel *monthLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(yearLabel.frame), KTipsW, 26); monthLabel.textColor = UIColor whiteColor; monthLabel.textAlignment = NSTextAlignmentCenter; monthLabel.font = UIFont systemFontOfS

20、ize:26.0f; tipsView addSubview:monthLabel; self.monthLabel = monthLabel; /日期标签 UILabel *dayLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(monthLabel.frame) + 30, KTipsW, 120); dayLabel.textColor = UIColor whiteColor; dayLabel.textAlignment = NSTextAlignmentCenter; dayLabel.font = UI

21、Font systemFontOfSize:76.0f; tipsView addSubview:dayLabel; self.dayLabel = dayLabel; CGFloat yearBtnW = 70.0f; CGFloat monthbtnW = 60.0f; CGFloat todayBtnW = 70.0f; CGFloat padding = (self.bounds.size.width - KTipsW - yearBtnW - monthbtnW - todayBtnW - KBtnW * 2) * 0.25; /年份按钮 HWOptionButton *yearBt

22、n = HWOptionButton alloc initWithFrame:CGRectMake(KTipsW + padding, 0, yearBtnW, KBtnH); yearBtn.array = _yearArray; yearBtn.row = KShowYearsCount / 2; yearBtn.delegate = self; self addSubview:yearBtn; self.yearBtn = yearBtn; /上一月 UIButton *preBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetM

23、axX(yearBtn.frame) + padding, 0, KBtnW, KBtnH); preBtn setImage:UIImage imageNamed:left forState:UIControlStateNormal; preBtn addTarget:self action:selector(preBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:preBtn; /月份按钮 HWOptionButton *monthBtn = HWOptionButton alloc init

24、WithFrame:CGRectMake(CGRectGetMaxX(preBtn.frame), 0, monthbtnW, KBtnH); monthBtn.array = _monthArray; monthBtn.row = _month - 1; monthBtn.delegate = self; self addSubview:monthBtn; self.monthBtn = monthBtn; /下一月 UIButton *nextBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetMaxX(monthBtn.frame

25、), 0, KBtnW, KBtnH); nextBtn setImage:UIImage imageNamed:right forState:UIControlStateNormal; nextBtn addTarget:self action:selector(nextBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:nextBtn; /返回今天按钮 UIButton *backTodayBtn = UIButton alloc initWithFrame:CGRectMake(CGRectG

26、etMaxX(nextBtn.frame) + padding, 0, todayBtnW, KBtnH); backTodayBtn.titleLabel.font = UIFont systemFontOfSize:14.0f; backTodayBtn setTitleColor:UIColor blackColor forState:UIControlStateNormal; backTodayBtn setTitle:返回今天 forState:UIControlStateNormal; backTodayBtn addTarget:self action:selector(back

27、TodayBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:backTodayBtn; /星期标签 for (int i = 0; i _weekArray.count; i+) UILabel *week = UILabel alloc initWithFrame:CGRectMake(KTipsW + KBtnH * i, KBtnH, KBtnH, KBtnH); week.textAlignment = NSTextAlignmentCenter; week.text = _weekArr

28、ayi; self addSubview:week; /日历核心视图 UIView *calendarView = UIView alloc initWithFrame:CGRectMake(KTipsW, KBtnH * 2, KBtnW * 7, KBtnH * 6); self addSubview:calendarView; self.calendarView = calendarView; /每一个日期用一个按钮去创建,当一个月的第一天是星期六并且有31天时为最多个数,5行零2个,共37个 for (int i = 0; i KMaxCount; i+) CGFloat btnX =

29、 i % KCol * KBtnW; CGFloat btnY = i / KCol * KBtnH; UIButton *btn = UIButton alloc initWithFrame:CGRectMake(btnX, btnY, KBtnW, KBtnH); btn.tag = i + KBtnTag; btn.layer.cornerRadius = KBtnW * 0.5; btn.layer.masksToBounds = YES; btn setTitle:NSString stringWithFormat:%d, i + 1 forState:UIControlStateN

30、ormal; btn setTitleColor:UIColor blackColor forState:UIControlStateNormal; btn setTitleColor:KMainColor forState:UIControlStateSelected; btn setBackgroundImage:self imageWithColor:KbackColor forState:UIControlStateHighlighted; btn setBackgroundImage:self imageWithColor:KbackColor forState:UIControlS

31、tateSelected; btn addTarget:self action:selector(dateBtnOnClick:) forControlEvents:UIControlEventTouchUpInside; calendarView addSubview:btn; /确认按钮 UIButton *sureBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetMinX(backTodayBtn.frame), CGRectGetMaxY(calendarView.frame), yearBtnW, KBtnH); sureB

32、tn.titleLabel.font = UIFont systemFontOfSize:16.0f; sureBtn setTitle:确定 forState:UIControlStateNormal; sureBtn setTitleColor:KMainColor forState:UIControlStateNormal; sureBtn addTarget:self action:selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:sureBtn; /取消按钮 U

33、IButton *cancelBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetMinX(sureBtn.frame) - yearBtnW, CGRectGetMinY(sureBtn.frame), yearBtnW, KBtnH); cancelBtn.titleLabel.font = UIFont systemFontOfSize:16.0f; cancelBtn setTitle:取消 forState:UIControlStateNormal; cancelBtn setTitleColor:KMainColor for

34、State:UIControlStateNormal; cancelBtn addTarget:self action:selector(cancelBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:cancelBtn; /时间选择器 _timePicker = UIPickerView alloc init; _timePicker.backgroundColor = KMainColor; _timePicker.hidden = YES; _timePicker.delegate = sel

35、f; _timePicker.dataSource = self; self addSubview:_timePicker; /set方法 - (void)setShowTimePicker:(BOOL)showTimePicker _showTimePicker = showTimePicker; if (showTimePicker) _timePicker.hidden = NO; _dayLabel.frame = CGRectMake(0, CGRectGetMaxY(_monthLabel.frame) + 10, KTipsW, 120); _timePicker.frame =

36、 CGRectMake(10, CGRectGetMaxY(_dayLabel.frame), KTipsW - 20, 88); else _timePicker.hidden = YES; _dayLabel.frame = CGRectMake(0, CGRectGetMaxY(_monthLabel.frame) + 30, 200, 120); /上一月按钮点击事件 - (void)preBtnOnClick if (_month = 1) if (_yearBtn.row = 0) return; _year -; _month = 12; _yearBtn.row -; else

37、 _month -; _monthBtn.row = _month - 1; self reloadData; /下一月按钮点击事件 - (void)nextBtnOnClick if (_month = 12) if (_yearBtn.row = KShowYearsCount - 1) return; _year +; _month = 1; _yearBtn.row +; else _month +; _monthBtn.row = _month - 1; self reloadData; /返回今天 - (void)backTodayBtnOnClick _year = _curre

38、ntYear; _month = _currentMonth; _monthBtn.row = _month - 1; _yearBtn.row = KShowYearsCount / 2; self reloadData; /刷新数据 - (void)reloadData NSInteger totalDays = self numberOfDaysInMonth; NSInteger firstDay = self firstDayOfWeekInMonth; _yearLabel.text = NSString stringWithFormat:%ld, _year; _monthLab

39、el.text = NSString stringWithFormat:%ld月, _month; _yearBtn.title = NSString stringWithFormat:%ld年, _year; _monthBtn.title = NSString stringWithFormat:%ld月, _month; for (int i = 0; i KMaxCount; i+) UIButton *btn = (UIButton *)self.calendarView viewWithTag:i + KBtnTag; btn.selected = NO; if (i totalDa

40、ys + firstDay - 2) btn.enabled = NO; btn setTitle: forState:UIControlStateNormal; else if (_year = _currentYear & _month = _currentMonth) if (btn.tag - KBtnTag - (firstDay - 2) = _currentDay) btn.selected = YES; _day = _currentDay; _weekLabel.text = NSString stringWithFormat:星期%, _weekArray(btn.tag

41、- KBtnTag) % 7; _dayLabel.text = NSString stringWithFormat:%ld, _day; else if (i = firstDay - 1) btn.selected = YES; _day = btn.tag - KBtnTag - (firstDay - 2); _weekLabel.text = NSString stringWithFormat:星期%, _weekArray(btn.tag - KBtnTag) % 7; _dayLabel.text = NSString stringWithFormat:%ld, _day; bt

42、n.enabled = YES; btn setTitle:NSString stringWithFormat:%ld, i - (firstDay - 1) + 1 forState:UIControlStateNormal; /获取当前时间 - (void)getCurrentDate NSDateComponents *components = NSCalendar currentCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | N

43、SCalendarUnitMinute fromDate:NSDate date; _year = components year; _month = components month; _day = components day; _hour = components hour; _minute = components minute; /根据选中日期,获取相应NSDate - (NSDate *)getSelectDate /初始化NSDateComponents,设置为选中日期 NSDateComponents *dateComponents = NSDateComponents all

44、oc init; dateComponents.year = _year; dateComponents.month = _month; return NSCalendar alloc initWithCalendarIdentifier:NSCalendarIdentifierGregorian dateFromComponents:dateComponents; /获取目标月份的天数 - (NSInteger)numberOfDaysInMonth /获取选中日期月份的天数 return NSCalendar currentCalendar rangeOfUnit:NSCalendarUn

45、itDay inUnit:NSCalendarUnitMonth forDate:self getSelectDate.length; /获取目标月份第一天星期几 - (NSInteger)firstDayOfWeekInMonth /获取选中日期月份第一天星期几,因为默认日历顺序为“日一二三四五六”,所以这里返回的1对应星期日,2对应星期一,依次类推 return NSCalendar currentCalendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfYear forDate:self getSelectDate; /根据颜色返回图片 - (UIImage *)imageWithColor:(UIColor *)color CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(con

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论