服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS自定义日历控件的简单实现过程

iOS自定义日历控件的简单实现过程

2021-02-03 14:37小姐贫僧光天化日 IOS

这篇文章主要介绍了iOS自定义日历控件的简单实现过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

因为程序要求要插入一个日历控件,该空间的要求是从当天开始及以后的六个月内的日历,上网查资料基本上都说只要获取两个条件(当月第一天周几和本月一共有多少天)就可以实现一个简单的日历,剩下的靠自己的简单逻辑就ok了,下面开始自己从开始到完成的整个过程

1.首先做nsdate类目,扩展一些方法让日期之间转换更加方便

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <foundation/foundation.h>
 
@interface nsdate (lywcalendar)
 
#pragma mark - 获取日
- (nsinteger)day:(nsdate *)date;
#pragma mark - 获取月
- (nsinteger)month:(nsdate *)date;
#pragma mark - 获取年
- (nsinteger)year:(nsdate *)date;
#pragma mark - 获取当月第一天周几
- (nsinteger)firstweekdayinthismonth:(nsdate *)date;
#pragma mark - 获取当前月有多少天
- (nsinteger)totaldaysinmonth:(nsdate *)date;
 
@end

下面一一实现这些方法

?
1
2
3
4
5
6
7
8
9
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
#import "nsdate+lywcalendar.h"
 
@implementation nsdate (lywcalendar)
 
/**
 *实现部分
 */
#pragma mark -- 获取日
- (nsinteger)day:(nsdate *)date{
 nsdatecomponents *components = [[nscalendar currentcalendar] components:(nscalendarunityear | nscalendarunitmonth | nscalendarunitday) fromdate:date];
 return components.day;
}
 
#pragma mark -- 获取月
- (nsinteger)month:(nsdate *)date{
 nsdatecomponents *components = [[nscalendar currentcalendar] components:(nscalendarunityear | nscalendarunitmonth | nscalendarunitday) fromdate:date];
 return components.month;
}
 
#pragma mark -- 获取年
- (nsinteger)year:(nsdate *)date{
 nsdatecomponents *components = [[nscalendar currentcalendar] components:(nscalendarunityear | nscalendarunitmonth | nscalendarunitday) fromdate:date];
 return components.year;
}
 
#pragma mark -- 获得当前月份第一天星期几
- (nsinteger)firstweekdayinthismonth:(nsdate *)date{
 nscalendar *calendar = [nscalendar currentcalendar];
 //设置每周的第一天从周几开始,默认为1,从周日开始
 [calendar setfirstweekday:1];//1.sun. 2.mon. 3.thes. 4.wed. 5.thur. 6.fri. 7.sat.
 nsdatecomponents *comp = [calendar components:(nscalendarunityear | nscalendarunitmonth | nscalendarunitday) fromdate:date];
 [comp setday:1];
 nsdate *firstdayofmonthdate = [calendar datefromcomponents:comp];
 nsuinteger firstweekday = [calendar ordinalityofunit:nscalendarunitweekday inunit:nscalendarunitweekofmonth fordate:firstdayofmonthdate];
 //若设置从周日开始算起则需要减一,若从周一开始算起则不需要减
 return firstweekday - 1;
}
#pragma mark -- 获取当前月共有多少天
 
- (nsinteger)totaldaysinmonth:(nsdate *)date{
 nsrange daysinlastmonth = [[nscalendar currentcalendar] rangeofunit:nscalendarunitday inunit:nscalendarunitmonth fordate:date];
 return daysinlastmonth.length;
}

接下来就要写逻辑部分了,在viewcontroller里面实现,先说思想,首先想到的是用collectionview,但是每个月天数不一样在日历中的显示就不一样,有时候有五行有时候有六行,既然要用自动填充就必须考虑到这一点,下面是代码实现

?
1
2
3
4
5
#import "viewcontroller.h"
#import "macro.h"
#import "nsdate+lywcalendar.h"
#import "lywcollectionviewcell.h"
#import "lywcollectionreusableview.h"

定义一些全局变量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static nsstring *cellid = @"cellid";
static nsstring *headerid = @"headerid";
static nsstring *footerid = @"footerid";
 
@implementation viewcontroller
{
 //自动布局
 uicollectionviewflowlayout *_layout;
 //表格视图
 uicollectionview *_collectionview;
 //当月第一天星期几
 nsinteger firstdayinmounthinweekly;
 nsmutablearray *_firstmounth;
 //容纳六个数组的数组
 nsmutablearray *_sixarray;
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//定义星期视图,若为周末则字体颜色为绿色
 self.automaticallyadjustsscrollviewinsets = no;//关闭自动适应
 nsarray *weektitlearray = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
 for (int i = 0; i < weektitlearray.count; i++) {
 uilabel *weektitlelable = [[uilabel alloc]initwithframe:cgrectmake(i * ((screenwidth/(weektitlearray.count))), 64, screenwidth/(weektitlearray.count ), 30)];
 if (i == 0 || i == 6) {
  weektitlelable.textcolor = [uicolor greencolor];
 }else{
  weektitlelable.textcolor = [uicolor blackcolor];
 }
 weektitlelable.text = [weektitlearray objectatindex:i];
 weektitlelable.textalignment = nstextalignmentcenter;
 [self.view addsubview:weektitlelable];
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//设置collectionview及自动布局,代理方法尤为重要
 _layout = [[uicollectionviewflowlayout alloc]init];
 //头部始终在顶端
 _layout.sectionheaderspintovisiblebounds = yes;
 //头部视图高度
 _layout.headerreferencesize = cgsizemake(414, 40);
 _layout.minimumlinespacing = 0;
 _layout.minimuminteritemspacing = 0;
 _collectionview = [[uicollectionview alloc]initwithframe:cgrectmake(0, 64 + 30, screenwidth, screenheight - 64 - 30) collectionviewlayout:_layout];
 _collectionview.backgroundcolor = [uicolor whitecolor];
 //注册表格
 [_collectionview registerclass:[lywcollectionviewcell class] forcellwithreuseidentifier:cellid];
 //注册头视图
 [_collectionview registerclass:[lywcollectionreusableview class] forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier:headerid];
 //注册尾视图
// [_collectionview registerclass:[uicollectionreusableview class] forcellwithreuseidentifier:footerid];
   _collectionview.delegate = self;
 _collectionview.datasource = self;
 [self.view addsubview:_collectionview];

逻辑部分,这里有个比较长的三项表达式

(daysinmounth > 29 && (firstdayinthismounth == 6 || firstdayinthismounth ==5) ? 42 : 35)

就是日历到底是六行还是七行,这就要根据日历的特性来判断了,如果当月天数大于29天并且当月第一天星期六(以这个程序的准则)或者星期天是返回六行剩下的返回三行,也有可能返回四行的,但是就这个程序来说是不可能的也就不需要做判断了

 

?
1
2
3
4
5
6
7
8
9
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
//numbermounthes 为宏定义,表示要显示月的个数,程序要求是六个月,所以宏定义为六
  //#define numbermounthes 6 //想要展示的月数
 
//创建六个数组,并将这六个数组装入大数组中
 _sixarray = [[nsmutablearray alloc]init];
 for (int i = 0; i < numbermounthes ; i++ ) {
 nsmutablearray *array = [[nsmutablearray alloc]init];
 [_sixarray addobject:array];
 }
 //为六个数组写入每个月的日历信息
 for (int i = 0 ; i < numbermounthes; i++) {
 //获取月份
 int mounth = ((int)[currentdate month:currentdate] + i)%12;
 nsdatecomponents *components = [[nsdatecomponents alloc]init];
 //获取下个月的年月日信息,并将其转为date
 components.month = mounth;
 components.year = 2016 + mounth/12;
 components.day = 1;
 nscalendar *calendar = [nscalendar currentcalendar];
 nsdate *nextdate = [calendar datefromcomponents:components];
 //获取该月第一天星期几
 nsinteger firstdayinthismounth = [nextdate firstweekdayinthismonth:nextdate];
 //该月的有多少天daysinthismounth
 nsinteger daysinthismounth = [nextdate totaldaysinmonth:nextdate];
 nsstring *string = [[nsstring alloc]init];
 for (int j = 0; j < (daysinmounth > 29 && (firstdayinthismounth == 6 || firstdayinthismounth ==5) ? 42 : 35) ; j++) {
  if (j < firstdayinthismounth || j > daysinthismounth + firstdayinthismounth - 1) {
  string = @"";
  [[_sixarray objectatindex:i]addobject:string];
  }else{
  string = [nsstring stringwithformat:@"%ld",j - firstdayinthismounth + 1];
  [[_sixarray objectatindex:i]addobject:string];
  }
 }
 }

下面是代理方法

?
1
2
3
4
5
6
7
8
//这两个不用说,返回cell个数及section个数
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{
 return [[_sixarray objectatindex:section] count];
}
 
- (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview{
 return _sixarray.count;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
//这里是自定义cell,非常简单的自定义
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{
 lywcollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellid forindexpath:indexpath];
 uiview *blackgroundview = [[uiview alloc]initwithframe:cgrectmake(0, 0, cell.frame.size.width, cell.frame.size.height)];
 blackgroundview.backgroundcolor = [uicolor yellowcolor];
 cell.datelable.text = [[_sixarray objectatindex:indexpath.section]objectatindex:indexpath.row];
 nsdate *date = [[nsdate alloc]init];
 nsinteger day = [date day:date];
  //设置单击后的颜色
   cell.selectedbackgroundview = blackgroundview;
 return cell;
}

自定义cell  .h

?
1
2
3
4
5
6
7
8
9
#import <uikit/uikit.h>
 
@interface lywcollectionviewcell : uicollectionviewcell
 
@property (nonatomic,strong) uilabel *datelable;
 
- (instancetype)initwithframe:(cgrect)frame;
 
@end

.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import "lywcollectionviewcell.h"
 
@implementation lywcollectionviewcell
 
- (instancetype)initwithframe:(cgrect)frame{
 if (self == [super initwithframe:frame]) {
 _datelable = [[uilabel alloc] initwithframe:self.bounds];
 [_datelable settextalignment:nstextalignmentcenter];
 [_datelable setfont:[uifont systemfontofsize:17]];
 _datelable.textcolor = [uicolor blackcolor];
 [self addsubview:_datelable];
 }
 return self;
}
@end

接着代理

?
1
2
3
4
5
6
7
8
//cell大小及间距
- (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath{
 return cgsizemake(screenwidth/7, screenwidth/7);
}
 
- (uiedgeinsets)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout insetforsectionatindex:(nsinteger)section{
 return uiedgeinsetsmake(0, 0, 0, 0);
}

既然有日历,总得显示哪年哪月吧,前面已经注册表头视图了,这里只需要实现以下代理方法即可

collectionview有点不同其头视图也有单独的类,和cell一样先自定义headcell,也是非常简单的自定义

.h文件

?
1
2
3
4
5
6
7
8
9
#import <uikit/uikit.h>
 
@interface lywcollectionreusableview : uicollectionreusableview
 
@property (nonatomic,strong) uilabel *datelable;
 
- (instancetype)initwithframe:(cgrect)frame;
 
@end

.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import "lywcollectionreusableview.h"
 
@implementation lywcollectionreusableview
 
- (instancetype)initwithframe:(cgrect)frame{
 if (self == [super initwithframe:frame]) {
 _datelable = [[uilabel alloc] initwithframe:self.bounds];
 [_datelable settextalignment:nstextalignmentleft];
 [_datelable setfont:[uifont systemfontofsize:20]];
 _datelable.textcolor = [uicolor blackcolor];
 [self addsubview:_datelable];
 }
 return self;
}
 
@end

接着代理方法,这里也有个三项判断式,和上面的大同小异,主要是防止12月显示为0月

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath{
 if (kind == uicollectionelementkindsectionheader) {
 lywcollectionreusableview *headerrv = [collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:headerid forindexpath:indexpath];
 //自定义蓝色
 headerrv.backgroundcolor = dodger_blue;
 nsdate *currentdate = [[nsdate alloc]init];
 nsinteger year = ([currentdate month:currentdate] + indexpath.section)/12 + 2016;
 nsinteger mounth = ([currentdate month:currentdate] + indexpath.section) % 12 == 0 ? 12 : ([currentdate month:currentdate] + indexpath.section)%12;
 headerrv.datelable.text = [nsstring stringwithformat:@"%ld年%ld月",year,mounth];
 return headerrv;
 }else{
 return nil;
 }
}

还是代理,处理选中效果,选中的为黄色

?
1
2
3
4
5
6
7
8
9
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
- (void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{
 
 lywcollectionviewcell *cell = [self collectionview:_collectionview cellforitematindexpath:indexpath];
 
 nsdate *currentdate = [[nsdate alloc]init];
 
 //打印当前日期
 
 if (![cell.datelable.text isequaltostring:@""]) {
 
 nsinteger year = ([currentdate month:currentdate] + indexpath.section)/12 + 2016;
 
 nsinteger mounth = ([currentdate month:currentdate] + indexpath.section)%12;
 
 nsinteger day = [cell.datelable.text intvalue];
 
 nslog(@"%ld年%02ld月%02ld日",year,mounth,day);
 
 }
 
 //排除空值cell
 
 //获取月份
 
 nsinteger mounth = ([currentdate month:currentdate] + indexpath.section) % 12 == 0 ? 12 : ([currentdate month:currentdate] + indexpath.section)%12;
 
 nsdatecomponents *components = [[nsdatecomponents alloc]init];
 
 components.month = mounth;
 
 components.year = 2016 + mounth/12;
 
 components.day = 1;
 
 nscalendar *calendar = [nscalendar currentcalendar];
 
 nsdate *nextdate = [calendar datefromcomponents:components];
 
 //获取该月第一天星期几
 
 nsinteger firstdayinthismounth = [nextdate firstweekdayinthismonth:nextdate];
 
 //该月的有多少天daysinthismounth
 
 nsinteger daysinthismounth = [nextdate totaldaysinmonth:nextdate];
 
 if ((indexpath.row < firstdayinthismounth || indexpath.row > daysinthismounth + firstdayinthismounth - 1)){
 
 //如果点击空表格则单击无效
 
 [collectionview cellforitematindexpath:indexpath].userinteractionenabled = no;
 
 [collectionview reloaddata];
 
 }
 
}

最后展示很烂的效果图:

iOS自定义日历控件的简单实现过程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01