timeManageSheetView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // timeManageSheetView.m
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/7.
  6. //
  7. #import "timeManageSheetView.h"
  8. #import "MonthModel.h"
  9. #import "MonthTableViewCell.h"
  10. #import "ADHTuanCan-Swift.h"
  11. @interface timeManageSheetView ()<UITableViewDelegate,UITableViewDataSource>
  12. /// tableviw父视图
  13. @property (weak, nonatomic) IBOutlet UIView *backView;
  14. /// 计数
  15. @property (weak, nonatomic) IBOutlet UILabel *countNumLab;
  16. @property(nonatomic,strong)UITableView *tableView;
  17. @property(nonatomic,strong)NSMutableArray *dataArray;
  18. @property(nonatomic,strong)UIView *weekView;
  19. /// 日历开始时间
  20. @property(nonatomic,strong)NSDate * viewBeginDate;
  21. /// 选择开始时间
  22. @property(nonatomic,strong)NSDate * startDate;
  23. /// 选择结束时间
  24. @property(nonatomic,strong)NSDate * endDate;
  25. @property(nonatomic,strong)DayModel * startDayModel;
  26. @property(nonatomic,strong)DayModel * endDayModel;
  27. @end
  28. @implementation timeManageSheetView
  29. - (void)awakeFromNib{
  30. [super awakeFromNib];
  31. // self.backgroundColor = [UIColor whiteColor];
  32. [self.backView addSubview:self.weekView];
  33. [self.backView addSubview:self.tableView];
  34. self.dataArray = [NSMutableArray array];
  35. }
  36. - (void)dealloc
  37. {
  38. NSLog(@"%s",__FUNCTION__);
  39. }
  40. /////////////////////////////////////创建视图///////////////////////////////////////////
  41. #pragma mark - 创建主视图
  42. -(UITableView *)tableView{
  43. if (!_tableView) {
  44. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width, 390) style:UITableViewStylePlain];
  45. _tableView.delegate = self;
  46. _tableView.dataSource = self;
  47. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  48. _tableView.showsVerticalScrollIndicator = NO;
  49. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellID"];
  50. }
  51. return _tableView;
  52. }
  53. #pragma mark - 创建星期视图
  54. -(UIView *)weekView{
  55. if (!_weekView) {
  56. _weekView = [[UIView alloc]initWithFrame:CGRectMake(-1, 0, self.bounds.size.width+2, 40)];
  57. NSMutableArray * title = [NSMutableArray array];
  58. NSArray *chtitle = @[@"一",@"二",@"三",@"四",@"五",@"六",@"日"];
  59. NSArray *entitle = @[@"Mon",@"Tues",@"Wed",@"Thur",@"Fri",@"Sat",@"Sun"];
  60. if (LanguagesUtil.isEnLanguage) {
  61. [title setArray:entitle];
  62. }else{
  63. [title setArray:chtitle];
  64. }
  65. for (int i =0 ; i < 7 ; i++) {
  66. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width/7*i+1, 0, self.bounds.size.width/7, _weekView.bounds.size.height)];
  67. label.textAlignment = NSTextAlignmentCenter;
  68. label.text = title[i];
  69. if (i>4) {
  70. label.textColor = [UIColor orangeColor];
  71. }
  72. [_weekView addSubview:label];
  73. }
  74. _weekView.backgroundColor = [UIColor whiteColor];
  75. _weekView.layer.borderWidth = 1;
  76. _weekView.layer.masksToBounds = YES;
  77. _weekView.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1].CGColor;
  78. }
  79. return _weekView;
  80. }
  81. /////////////////////////////////////点击事件///////////////////////////////////////////
  82. #pragma mark - 确定事件返回
  83. - (IBAction)saveAction:(UIButton *)sender {
  84. if (self.startDate == nil) {
  85. return;
  86. }
  87. if (self.endDate == nil) {
  88. return;
  89. }
  90. [self selectedCheckDate];
  91. }
  92. -(void)selectedCheckDate{
  93. NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate];
  94. NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
  95. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  96. NSString *startDateStr = [dateFormatter stringFromDate:self.startDate];
  97. NSString *endDateStr = [dateFormatter stringFromDate:self.endDate];
  98. NSString *daysStr = [NSString stringWithFormat:@"%ld",days + 1];
  99. [self hidden];
  100. if (_selectCheckDateBlock) {
  101. _selectCheckDateBlock(startDateStr,endDateStr,daysStr);
  102. }
  103. }
  104. /////////////////////////////////////代理方法///////////////////////////////////////////
  105. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  106. return self.dataArray.count;
  107. }
  108. -(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  109. MonthModel *model = self.dataArray[indexPath.row];
  110. return model.cellHight;
  111. }
  112. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  113. MonthTableViewCell *cell = [MonthTableViewCell cellWithTableView:tableView];
  114. cell.model = self.dataArray[indexPath.row];
  115. __weak typeof(self) weakSelf = self;
  116. cell.selectedDay = ^(DayModel *returnDaymodel){
  117. NSDate * nowdate = NSDate.now;
  118. self.countNumLab.text = @"";
  119. if ([self compareDate:returnDaymodel.dayDate withDate:self.startDate] == 1) {
  120. for (MonthModel *Mo in self.dataArray) {
  121. for (DayModel *mo in Mo.days) {
  122. mo.state = DayModelStateNormal;
  123. mo.Daysort = 0;
  124. }
  125. }
  126. self.startDate = nil;
  127. self.endDate = nil;
  128. self.startDate = returnDaymodel.dayDate;
  129. returnDaymodel.state = DayModelStateStart;
  130. returnDaymodel.Daysort = 1;
  131. returnDaymodel.isEndTimeOK = NO;
  132. self.startDayModel = returnDaymodel;
  133. [self.tableView reloadData];
  134. return;
  135. }
  136. if ((self.startDate != nil) && (self.endDate != nil)){
  137. for (MonthModel *Mo in self.dataArray) {
  138. for (DayModel *mo in Mo.days) {
  139. mo.state = DayModelStateNormal;
  140. mo.Daysort = 0;
  141. }
  142. }
  143. self.startDate = nil;
  144. self.endDate = nil;
  145. self.startDate = returnDaymodel.dayDate;
  146. returnDaymodel.state = DayModelStateStart;
  147. returnDaymodel.Daysort = 1;
  148. returnDaymodel.isEndTimeOK = NO;
  149. self.startDayModel = returnDaymodel;
  150. [self.tableView reloadData];
  151. return;
  152. }
  153. if (self.endDate == nil) {
  154. self.endDate = returnDaymodel.dayDate;
  155. for (MonthModel *Mo in self.dataArray) {
  156. for (DayModel *mo in Mo.days) {
  157. mo.state = DayModelStateNormal;
  158. mo.Daysort = 0;
  159. if ([self compareDate:mo.dayDate withDate:self.endDate] == 1 && [self compareDate:mo.dayDate withDate:self.startDate] == -1){
  160. mo.state = DayModelStateSelected;
  161. NSInteger days = [self calcDaysFromBegin:self.startDate end:mo.dayDate];
  162. mo.Daysort = days + 1;
  163. }
  164. if (mo.dayDate == self.startDate) {
  165. self.startDate = mo.dayDate;
  166. self.startDayModel = mo;
  167. mo.state = DayModelStateStart;
  168. mo.Daysort = 1;
  169. mo.isEndTimeOK = YES;
  170. }
  171. }
  172. }
  173. NSInteger days1 = [self calcDaysFromBegin:self.startDate end:returnDaymodel.dayDate];
  174. returnDaymodel.Daysort = days1 + 1;
  175. returnDaymodel.state = DayModelStateEnd;
  176. self.endDayModel = returnDaymodel;
  177. NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate];
  178. self.countNumLab.text = [LanguagesUtil createTextByCtext:[NSString stringWithFormat:@"共计%d天",days + 1] Etext:[NSString stringWithFormat:@"%d days in total",days + 1]];
  179. [self.tableView reloadData];
  180. return;
  181. }
  182. };
  183. return cell;
  184. }
  185. /////////////////////////////////////数据处理///////////////////////////////////////////
  186. #pragma mark - 懒加载数据源
  187. -(void)setDateArr{
  188. [self.dataArray removeAllObjects];
  189. NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate];
  190. self.countNumLab.text = [LanguagesUtil createTextByCtext:[NSString stringWithFormat:@"共计%d天",days] Etext:[NSString stringWithFormat:@"%d days in total",days]];
  191. NSDate *nowdate = NSDate.now;
  192. NSInteger toYear = [self getDataFromDate:nowdate type:@"year"];
  193. NSInteger toMonth = [self getDataFromDate:nowdate type:@"month"];
  194. for (int i = 0; i<2; i++) {
  195. if (i == 0) {
  196. MonthModel * monthModel = [[MonthModel alloc] init];
  197. monthModel.year = toYear;
  198. monthModel.month = toMonth;
  199. NSMutableArray *days = [NSMutableArray array ];
  200. NSInteger starNum = [self getDataFromDate :self.viewBeginDate type:@"day"];
  201. for (NSInteger i = starNum ; i <=[self totaldaysInMonth:nowdate]; i++) {
  202. DayModel *dayModel = [[DayModel alloc]init];
  203. dayModel.dayDate = [self dateWithYear:monthModel.year month:monthModel.month day:i];
  204. dayModel.day = i;
  205. dayModel.month = monthModel.month;
  206. dayModel.year = monthModel.year;
  207. dayModel.dayOfTheWeek = [self getDataFromDate:dayModel.dayDate type:@"week"];
  208. dayModel.isToday = i==starNum;
  209. dayModel.state = DayModelStateNormal;
  210. dayModel.Daysort = 0;
  211. if ([self compareDate:dayModel.dayDate withDate:nowdate] == 1) {
  212. dayModel.isStatePast = YES;
  213. }
  214. if ([self compareDate:dayModel.dayDate withDate:self.startDate] == 0) {
  215. dayModel.state = DayModelStateStart;
  216. dayModel.Daysort = 1;
  217. dayModel.isEndTimeOK = YES;
  218. self.startDayModel = dayModel;
  219. }
  220. if ([self compareDate:dayModel.dayDate withDate:self.endDate] == 0) {
  221. dayModel.state = DayModelStateEnd;
  222. dayModel.Daysort = [NSString stringWithFormat:@"%ld",[self calcDaysFromBegin:self.startDate end:dayModel.dayDate]].intValue + 1;
  223. //差值需要加1
  224. self.endDayModel = dayModel;
  225. }
  226. if ([self compareDate:dayModel.dayDate withDate:self.endDate] == 1 && [self compareDate:dayModel.dayDate withDate:self.startDate] == -1){
  227. dayModel.state = DayModelStateSelected;
  228. dayModel.Daysort = [NSString stringWithFormat:@"%ld",[self calcDaysFromBegin:self.startDate end:dayModel.dayDate]].intValue + 1;
  229. //差值需要加1
  230. }
  231. [days addObject:dayModel];
  232. }
  233. monthModel.days = days;
  234. DayModel *m = days.firstObject;
  235. NSInteger lineCount = 1;
  236. NSInteger oneLineCoune =( 7 - m.dayOfTheWeek + 2 ) % 7;
  237. if (oneLineCoune == 0) {
  238. oneLineCoune = 7;
  239. }
  240. NSInteger count = days.count - oneLineCoune;
  241. if (count%7==0) {
  242. lineCount = lineCount + count/7 ;
  243. }else{
  244. lineCount = lineCount + count/7 + 1 ;
  245. }
  246. monthModel.cellNum = lineCount * 7;
  247. monthModel.cellStartNum = 7 - oneLineCoune ;
  248. monthModel.cellHight = 60 + 60 * lineCount + 2 * (lineCount + 1);
  249. [_dataArray addObject:monthModel];
  250. toMonth++;
  251. }else{
  252. if (toMonth == 13) {
  253. toMonth = 1;
  254. toYear += 1;
  255. }
  256. NSDate *toDate = [self dateWithYear:toYear month:toMonth day:1];
  257. MonthModel * monthModel = [[MonthModel alloc] init];
  258. monthModel.year = [self getDataFromDate:toDate type:@"year"];
  259. monthModel.month = [self getDataFromDate:toDate type:@"month"];
  260. NSMutableArray *days = [NSMutableArray array ];
  261. for (NSInteger i = 1 ; i <=[self totaldaysInMonth:toDate]; i++) {
  262. DayModel *dayModel = [[DayModel alloc]init];
  263. dayModel.dayDate = [self dateWithYear:monthModel.year month:monthModel.month day:i];
  264. dayModel.day = i;
  265. dayModel.month = monthModel.month;
  266. dayModel.year = monthModel.year;
  267. dayModel.dayOfTheWeek = [self getDataFromDate:dayModel.dayDate type:@"week"];
  268. dayModel.isToday = NO;
  269. dayModel.state = DayModelStateNormal;
  270. [days addObject:dayModel];
  271. }
  272. monthModel.days = days;
  273. DayModel *m = days.firstObject;
  274. NSInteger lineCount = 1;
  275. NSInteger oneLineCoune =( 7 - m.dayOfTheWeek + 2 ) % 7;
  276. if (oneLineCoune == 0) {
  277. oneLineCoune = 7;
  278. }
  279. NSInteger count = days.count - oneLineCoune;
  280. if (count%7==0) {
  281. lineCount = lineCount + count/7 ;
  282. }else{
  283. lineCount = lineCount + count/7 + 1 ;
  284. }
  285. monthModel.cellNum = lineCount * 7;
  286. monthModel.cellStartNum = 7 - oneLineCoune ;
  287. monthModel.cellHight = 60 + 60 * lineCount + 2 * (lineCount + 1);
  288. [_dataArray addObject:monthModel];
  289. toMonth++;
  290. }
  291. }
  292. [self.tableView reloadData];
  293. }
  294. #pragma mark - 获取年,月,日,星期 注:日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。
  295. -(NSInteger )getDataFromDate:(NSDate *)date type:(NSString * )type{
  296. NSCalendar *calendar = nil;
  297. if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
  298. calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  299. }else{
  300. calendar = [NSCalendar currentCalendar];
  301. }
  302. NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday) fromDate:date];
  303. if ([type isEqualToString:@"year"]) {
  304. return components.year;
  305. }else if ([type isEqualToString:@"month"]) {
  306. return components.month;
  307. }else if ([type isEqualToString:@"day"]) {
  308. return components.day;
  309. }else if ([type isEqualToString:@"week"]) {
  310. return components.weekday;
  311. }else{
  312. return 0;
  313. }
  314. }
  315. #pragma mark -- 获取当前月共有多少天
  316. - (NSInteger)totaldaysInMonth:(NSDate *)date{
  317. NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
  318. return daysInLastMonth.length;
  319. }
  320. #pragma mark - 时间字符串转时间
  321. -(NSDate *)dateWithYear:(NSInteger )year month:(NSInteger )month day:(NSInteger )day
  322. {
  323. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  324. [formatter setDateFormat:@"yyyyMMdd"];
  325. return [formatter dateFromString:[NSString stringWithFormat:@"%ld%02ld%02ld",year,month,day]];
  326. }
  327. #pragma mark-日期比较
  328. -(NSInteger )compareDate:(NSDate *)date01 withDate:(NSDate *)date02{
  329. NSInteger ci;
  330. NSComparisonResult result = [date01 compare:date02];
  331. switch (result)
  332. {
  333. //date02比date01大
  334. case NSOrderedAscending: ci=1; break;
  335. //date02比date01小
  336. case NSOrderedDescending: ci=-1; break;
  337. //date02=date01
  338. case NSOrderedSame: ci=0; break;
  339. default: NSLog(@"erorr dates %@, %@", date02, date01); break;
  340. }
  341. return ci;
  342. }
  343. #pragma mark - <#content#> 计算两个日期之间的天数
  344. - (NSInteger) calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate{
  345. //创建日期格式化对象
  346. NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
  347. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  348. //取两个日期对象的时间间隔:
  349. //这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;
  350. NSTimeInterval time=[endDate timeIntervalSinceDate:beginDate];
  351. int days=((int)time)/(3600*24);
  352. //int hours=((int)time)%(3600*24)/3600;
  353. //NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];
  354. return days;
  355. }
  356. - (IBAction)closeAction:(UIButton *)sender {
  357. [self hidden];
  358. }
  359. -(void)showWithStaStr:(NSString *) staStr endStr:(NSString *)endStr beginDate:(NSDate *)beginDate{
  360. NSString * begYear = [staStr substringWithRange:NSMakeRange(0,4)];
  361. NSString * begMonth = [staStr substringWithRange:NSMakeRange(5,2)];
  362. NSString * begDay = [staStr substringWithRange:NSMakeRange(8,2)];
  363. NSString * endYear = [endStr substringWithRange:NSMakeRange(0,4)];
  364. NSString * endMonth = [endStr substringWithRange:NSMakeRange(5,2)];
  365. NSString * endDay = [endStr substringWithRange:NSMakeRange(8,2)];
  366. self.startDate = [self dateWithYear:begYear.intValue month:begMonth.intValue day:begDay.intValue];
  367. self.endDate = [self dateWithYear:endYear.intValue month:endMonth.intValue day:endDay.intValue];
  368. self.viewBeginDate = beginDate;
  369. [self setDateArr];
  370. self.frame = [UIApplication sharedApplication].keyWindow.bounds;
  371. [[UIApplication sharedApplication].keyWindow addSubview:self];
  372. }
  373. -(void)hidden{
  374. [self removeFromSuperview];
  375. }
  376. @end