经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS日历控件
来源:cnblogs  作者:翾燚  时间:2019/3/29 9:11:32  对本文有异议

项目需要,前一阵子重构了下iPad工程,添加了一个滚动无缝日历。

当时没有头绪,网上找了一个源码改吧改吧就上线了(参考链接),这个功能很多而且流畅性也特别好,推荐不会写的可以参考下。

这几天,活不太忙就把日历控件裁剪了下,做个最简单的滚动无缝日历。效果如下图:

 

日历可以左右滚动,点击某个日期后会变色,并且有回调。橘色的是标记日期,蓝色的是选择日期,蓝边的是当前日期,可以根据需要自行更改。

 

这个日历控件有两个比较复杂的地方:

  • UICollectionView默认情况下,横滚cell竖排竖滚cell横排,所以我们先要修改下cell的位置,自定义FlowLayout继承于UICollectionViewFlowLayout,重写它的prepareLayout方法。
    1. #import "EXCalendarCollectionViewFlowLayout.h"
    2.  
    3. @interface EXCalendarCollectionViewFlowLayout ()
    4. @property (nonatomic, strong) NSMutableArray *allAttributes;
    5. @end
    6.  
    7.  
    8. @implementation EXCalendarCollectionViewFlowLayout
    9. - (void)prepareLayout {
    10. [super prepareLayout];
    11. self.allAttributes = [NSMutableArray array];
    12. NSInteger sections = [self.collectionView numberOfSections];
    13. for (int i = 0; i < sections; i++) {
    14. // setup one section attributes.
    15. NSMutableArray *tmpArray = [NSMutableArray array];
    16. NSInteger count = [self.collectionView numberOfItemsInSection:i];
    17. for (NSInteger j = 0; j < count; j++) {
    18. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
    19. UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
    20. [tmpArray addObject:attributes];
    21. }
    22. [self.allAttributes addObject:tmpArray];
    23. }
    24. }
    25. - (CGSize)collectionViewContentSize {
    26. return [super collectionViewContentSize];
    27. }
    28. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    29. NSInteger item = indexPath.item;
    30. NSInteger x;
    31. NSInteger y;
    32. // 根据item的序号计算出item的行列位置
    33. [self targetPositionWithItem:item resultX:&x resultY:&y];
    34. // 根据已得出的item的行列位置,将item放入indexPath中对应的位置。
    35. NSInteger item2 = [self orignItemAtX:x y:y];
    36. NSIndexPath *theNewIndexPath = [NSIndexPath indexPathForItem:item2 inSection:indexPath.section];
    37. UICollectionViewLayoutAttributes *theNewAttr = [super layoutAttributesForItemAtIndexPath:theNewIndexPath];
    38. theNewAttr.indexPath = indexPath;
    39. return theNewAttr;
    40. }
    41. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    42. NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    43. NSMutableArray *tmp = [NSMutableArray array];
    44. for (UICollectionViewLayoutAttributes *attr in attributes) {
    45. for (NSMutableArray *attributes in self.allAttributes)
    46. {
    47. for (UICollectionViewLayoutAttributes *attr2 in attributes) {
    48. if (attr.indexPath.item == attr2.indexPath.item) {
    49. [tmp addObject:attr2];
    50. break;
    51. }
    52. }
    53. }
    54. }
    55. return tmp;
    56. }
    57. // 根据item计算目标item的位置。
    58. - (void)targetPositionWithItem:(NSInteger)item
    59. resultX:(NSInteger *)x
    60. resultY:(NSInteger *)y {
    61. // NSInteger page = item / (self.itemCountPerRow * self.rowCountPerPage);
    62. NSInteger theX = item % self.itemCountPerRow;
    63. NSInteger theY = item / self.itemCountPerRow;
    64. if (x != NULL) {
    65. *x = theX;
    66. }
    67. if (y != NULL) {
    68. *y = theY;
    69. }
    70. }
    71. - (NSInteger)orignItemAtX:(NSInteger)x
    72. y:(NSInteger)y {
    73. NSInteger item = x * self.rowCountPerPage + y;
    74. return item;
    75. }
    76. @end
    View Code

     


  • 当你在当前月份点击了一个日期,滑到其他月份,然后要对刚才选择的月份的效果进行更改时,比较麻烦。刚开始我在didSelectItemAtIndexPath委托方法中用cellForItemAtIndexPath进行获取时,不可见的cell获取不到返回的是空,然后在如何获取不可见的cell问题上纠结了两天,最终换了个解决方案,在cellForItemAtIndexPath中进行了判断,解决了这个问题,当然点击后直接有响应跳转的话,刚才这个功能就很鸡肋了。具体看代码吧。

 

源码地址:https://github.com/zhanghua0926/EXCalendar

 

原文链接:http://www.cnblogs.com/xuanyishare/p/10615624.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号