猎豆优选

LXCalendarView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // LXCalendarView.m
  3. // LXCalendar
  4. //
  5. // Created by chenergou on 2017/11/2.
  6. // Copyright © 2017年 漫漫. All rights reserved.
  7. //
  8. #import "LXCalendarView.h"
  9. #import "LXCalendarHearder.h"
  10. #import "LXCalendarWeekView.h"
  11. #import "LXCalenderCell.h"
  12. #import "LXCalendarMonthModel.h"
  13. #import "NSDate+GFCalendar.h"
  14. #import "LXCalendarDayModel.h"
  15. #import "UIView+LX_Frame.h"
  16. @interface LXCalendarView()<UICollectionViewDelegate,UICollectionViewDataSource>
  17. @property(nonatomic,strong)LXCalendarHearder *calendarHeader; //头部
  18. @property(nonatomic,strong)LXCalendarWeekView *calendarWeekView;//周
  19. @property(nonatomic,strong)UICollectionView *collectionView;//日历
  20. @property(nonatomic,strong)NSMutableArray *monthdataA;//当月的模型集合
  21. @property(nonatomic,strong)NSDate *currentMonthDate;//当月的日期
  22. @property(nonatomic,strong)UISwipeGestureRecognizer *leftSwipe;//左滑手势
  23. @property(nonatomic,strong)UISwipeGestureRecognizer *rightSwipe;//右滑手势
  24. @property(nonatomic,strong)LXCalendarDayModel *selectModel;
  25. @end
  26. @implementation LXCalendarView
  27. -(instancetype)initWithFrame:(CGRect)frame{
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. self.currentMonthDate = [NSDate date];
  31. [self setup];
  32. }
  33. return self;
  34. }
  35. -(void)dealData{
  36. [self responData];
  37. }
  38. -(void)setup{
  39. [self addSubview:self.calendarHeader];
  40. WeakSelf(weakSelf);
  41. self.calendarHeader.leftClickBlock = ^{
  42. [weakSelf rightSlide];
  43. };
  44. self.calendarHeader.rightClickBlock = ^{
  45. [weakSelf leftSlide];
  46. };
  47. [self addSubview:self.calendarWeekView];
  48. [self addSubview:self.collectionView];
  49. self.lx_height = self.collectionView.lx_bottom;
  50. //添加左滑右滑手势
  51. self.leftSwipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
  52. self.leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
  53. [self.collectionView addGestureRecognizer:self.leftSwipe];
  54. self.rightSwipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
  55. self.rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
  56. [self.collectionView addGestureRecognizer:self.rightSwipe];
  57. }
  58. #pragma mark --左滑手势--
  59. -(void)leftSwipe:(UISwipeGestureRecognizer *)swipe{
  60. [self leftSlide];
  61. }
  62. #pragma mark --左滑处理--
  63. -(void)leftSlide{
  64. self.currentMonthDate = [self.currentMonthDate nextMonthDate];
  65. [self performAnimations:kCATransitionFromRight];
  66. [self responData];
  67. }
  68. #pragma mark --右滑处理--
  69. -(void)rightSlide{
  70. self.currentMonthDate = [self.currentMonthDate previousMonthDate];
  71. [self performAnimations:kCATransitionFromLeft];
  72. [self responData];
  73. }
  74. #pragma mark --右滑手势--
  75. -(void)rightSwipe:(UISwipeGestureRecognizer *)swipe{
  76. [self rightSlide];
  77. }
  78. #pragma mark--动画处理--
  79. - (void)performAnimations:(NSString *)transition{
  80. CATransition *catransition = [CATransition animation];
  81. catransition.duration = 0.5;
  82. [catransition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  83. catransition.type = kCATransitionPush; //choose your animation
  84. catransition.subtype = transition;
  85. [self.collectionView.layer addAnimation:catransition forKey:nil];
  86. }
  87. #pragma mark--数据以及更新处理--
  88. -(void)responData{
  89. [self.monthdataA removeAllObjects];
  90. NSDate *previousMonthDate = [self.currentMonthDate previousMonthDate];
  91. // NSDate *nextMonthDate = [self.currentMonthDate nextMonthDate];
  92. LXCalendarMonthModel *monthModel = [[LXCalendarMonthModel alloc]initWithDate:self.currentMonthDate];
  93. LXCalendarMonthModel *lastMonthModel = [[LXCalendarMonthModel alloc]initWithDate:previousMonthDate];
  94. // LXCalendarMonthModel *nextMonthModel = [[LXCalendarMonthModel alloc]initWithDate:nextMonthDate];
  95. self.calendarHeader.dateStr = [NSString stringWithFormat:@"%ld年%ld月",monthModel.year,monthModel.month];
  96. NSInteger firstWeekday = monthModel.firstWeekday;
  97. NSInteger totalDays = monthModel.totalDays;
  98. for (int i = 0; i <42; i++) {
  99. LXCalendarDayModel *model =[[LXCalendarDayModel alloc]init];
  100. //配置外面属性
  101. [self configDayModel:model];
  102. model.firstWeekday = firstWeekday;
  103. model.totalDays = totalDays;
  104. model.month = monthModel.month;
  105. model.year = monthModel.year;
  106. //上个月的日期
  107. if (i < firstWeekday) {
  108. model.day = lastMonthModel.totalDays - (firstWeekday - i) + 1;
  109. model.isLastMonth = YES;
  110. }
  111. //当月的日期
  112. if (i >= firstWeekday && i < (firstWeekday + totalDays)) {
  113. model.day = i -firstWeekday +1;
  114. model.isCurrentMonth = YES;
  115. //标识是今天
  116. if ((monthModel.month == [[NSDate date] dateMonth]) && (monthModel.year == [[NSDate date] dateYear])) {
  117. if (i == [[NSDate date] dateDay] + firstWeekday - 1) {
  118. model.isToday = YES;
  119. }
  120. }
  121. }
  122. //下月的日期
  123. if (i >= (firstWeekday + monthModel.totalDays)) {
  124. model.day = i -firstWeekday - monthModel.totalDays +1;
  125. model.isNextMonth = YES;
  126. }
  127. [self.monthdataA addObject:model];
  128. }
  129. [self.monthdataA enumerateObjectsUsingBlock:^(LXCalendarDayModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
  130. if ((obj.year == self.selectModel.year) && (obj.month == self.selectModel.month) && (obj.day == self.selectModel.day)) {
  131. obj.isSelected = YES;
  132. }
  133. }];
  134. [self.collectionView reloadData];
  135. }
  136. -(void)configDayModel:(LXCalendarDayModel *)model{
  137. //配置外面属性
  138. model.isHaveAnimation = self.isHaveAnimation;
  139. model.currentMonthTitleColor = self.currentMonthTitleColor;
  140. model.lastMonthTitleColor = self.lastMonthTitleColor;
  141. model.nextMonthTitleColor = self.nextMonthTitleColor;
  142. model.selectBackColor = self.selectBackColor;
  143. model.isHaveAnimation = self.isHaveAnimation;
  144. model.todayTitleColor = self.todayTitleColor;
  145. model.isShowLastAndNextDate = self.isShowLastAndNextDate;
  146. }
  147. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  148. return self.monthdataA.count;
  149. }
  150. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. NSString *cellIndentifier = @"cell";
  153. LXCalenderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
  154. if (!cell) {
  155. cell =[[LXCalenderCell alloc]init];
  156. }
  157. LXCalendarDayModel *model = self.monthdataA[indexPath.row];
  158. cell.model = model;
  159. // if (model.isToday) {
  160. // self.selectBlock(model.year, model.month, model.day);
  161. // }
  162. cell.backgroundColor =[UIColor whiteColor];
  163. return cell;
  164. }
  165. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. LXCalendarDayModel *model = self.monthdataA[indexPath.row];
  168. model.isSelected = YES;
  169. //选中的day
  170. self.selectModel = model;
  171. [self.monthdataA enumerateObjectsUsingBlock:^(LXCalendarDayModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  172. if (obj != model) {
  173. obj.isSelected = NO;
  174. }
  175. }];
  176. if (self.selectBlock) {
  177. self.selectBlock(model.year, model.month, model.day);
  178. }
  179. [collectionView reloadData];
  180. }
  181. -(void)layoutSubviews{
  182. [super layoutSubviews];
  183. self.calendarHeader.frame = CGRectMake(0, 0, self.lx_width, 50);
  184. }
  185. #pragma mark---懒加载
  186. -(LXCalendarHearder *)calendarHeader{
  187. if (!_calendarHeader) {
  188. _calendarHeader =[LXCalendarHearder showView];
  189. _calendarHeader.frame = CGRectMake(0, 0, self.lx_width, 50);
  190. _calendarHeader.backgroundColor =[UIColor whiteColor];
  191. }
  192. return _calendarHeader;
  193. }
  194. -(LXCalendarWeekView *)calendarWeekView{
  195. if (!_calendarWeekView) {
  196. _calendarWeekView =[[LXCalendarWeekView alloc]initWithFrame:CGRectMake(10, self.calendarHeader.lx_bottom, self.lx_width-20, 50)];
  197. _calendarWeekView.weekTitles = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
  198. }
  199. return _calendarWeekView;
  200. }
  201. -(UICollectionView *)collectionView{
  202. if (!_collectionView) {
  203. UICollectionViewFlowLayout *flow =[[UICollectionViewFlowLayout alloc]init];
  204. //325*403
  205. flow.minimumInteritemSpacing = 0;
  206. flow.minimumLineSpacing = 0;
  207. flow.sectionInset =UIEdgeInsetsMake(0 , 0, 0, 0);
  208. CGFloat itemHeight = (self.lx_width-21)/7;
  209. flow.itemSize = CGSizeMake((self.lx_width-21)/7, itemHeight);
  210. _collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(10, self.calendarWeekView.lx_bottom, self.lx_width-20, 6 * itemHeight) collectionViewLayout:flow];
  211. _collectionView.delegate = self;
  212. _collectionView.dataSource = self;
  213. _collectionView.showsVerticalScrollIndicator = NO;
  214. _collectionView.showsHorizontalScrollIndicator = NO;
  215. _collectionView.scrollsToTop = YES;
  216. _collectionView.backgroundColor = [UIColor whiteColor];
  217. UINib *nib = [UINib nibWithNibName:@"LXCalenderCell" bundle:nil];
  218. [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
  219. }
  220. return _collectionView;
  221. }
  222. -(NSMutableArray *)monthdataA{
  223. if (!_monthdataA) {
  224. _monthdataA =[NSMutableArray array];
  225. }
  226. return _monthdataA;
  227. }
  228. /*
  229. * 当前月的title颜色
  230. */
  231. -(void)setCurrentMonthTitleColor:(UIColor *)currentMonthTitleColor{
  232. _currentMonthTitleColor = currentMonthTitleColor;
  233. }
  234. /*
  235. * 上月的title颜色
  236. */
  237. -(void)setLastMonthTitleColor:(UIColor *)lastMonthTitleColor{
  238. _lastMonthTitleColor = lastMonthTitleColor;
  239. }
  240. /*
  241. * 下月的title颜色
  242. */
  243. -(void)setNextMonthTitleColor:(UIColor *)nextMonthTitleColor{
  244. _nextMonthTitleColor = nextMonthTitleColor;
  245. }
  246. /*
  247. * 选中的背景颜色
  248. */
  249. -(void)setSelectBackColor:(UIColor *)selectBackColor{
  250. _selectBackColor = selectBackColor;
  251. }
  252. /*
  253. * 选中的是否动画效果
  254. */
  255. -(void)setIsHaveAnimation:(BOOL)isHaveAnimation{
  256. _isHaveAnimation = isHaveAnimation;
  257. }
  258. /*
  259. * 是否禁止手势滚动
  260. */
  261. -(void)setIsCanScroll:(BOOL)isCanScroll{
  262. _isCanScroll = isCanScroll;
  263. self.leftSwipe.enabled = self.rightSwipe.enabled = isCanScroll;
  264. }
  265. /*
  266. * 是否显示上月,下月的按钮
  267. */
  268. -(void)setIsShowLastAndNextBtn:(BOOL)isShowLastAndNextBtn{
  269. _isShowLastAndNextBtn = isShowLastAndNextBtn;
  270. self.calendarHeader.isShowLeftAndRightBtn = isShowLastAndNextBtn;
  271. }
  272. /*
  273. * 是否显示上月,下月的的数据
  274. */
  275. -(void)setIsShowLastAndNextDate:(BOOL)isShowLastAndNextDate{
  276. _isShowLastAndNextDate = isShowLastAndNextDate;
  277. }
  278. /*
  279. * 今日的title颜色
  280. */
  281. -(void)setTodayTitleColor:(UIColor *)todayTitleColor{
  282. _todayTitleColor = todayTitleColor;
  283. }
  284. @end