公司项目有签到日历的功能,可自定义设置最小日期及最大日期。设置选中日期的背景颜色和背景图片。可根据后台返回的数据实现几个选中的日期功能。
- (void)viewDidLoad {
[super viewDidLoad];
[self loadSignatureData];
self.navigationItem.title = @"每日签到";
self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}
- (void)setupCalendarViewCell
{
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(15, 360, KscreenWidth-30, 300)];
calendar.dataSource = self;
calendar.delegate = self;
calendar.appearance.weekdayTextColor=mainColor;
calendar.appearance.headerTitleColor=mainColor;
calendar.allowsMultipleSelection = YES;
[self.tableView addSubview:calendar];
self.calendar = calendar;
calendar.calendarHeaderView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
calendar.calendarWeekdayView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
calendar.appearance.selectionColor =[UIColor clearColor];//选中颜色
calendar.today = nil; // Hide the today circle
[calendar registerClass:[DIYCalendarCell class] forCellReuseIdentifier:@"cell"];
}
//定制今天的日期还有好多有趣的API可以自己去看看
- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date
{
if ([self.gregorian isDateInToday:date]) {
return @"今";
}
return nil;
}
- (void)dealloc
{
// NSLog(@"%s",__FUNCTION__);
}
#pragma mark - FSCalendarDataSource
//设置最小日期
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
return [self.dateFormatter dateFromString:@"2017-08-01"];
}
//最大
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:0 toDate:[NSDate date] options:0];
}
//定制cell
- (FSCalendarCell *)calendar:(FSCalendar *)calendar cellForDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
DIYCalendarCell *cell = [calendar dequeueReusableCellWithIdentifier:@"cell" forDate:date atMonthPosition:monthPosition];
//定制图片
//cell.circleImageView.image=[UIImage imageNamed:@"勾16"];
return cell;
}
#pragma mark - FSCalendarDelegate
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
//当天不能点击
if ([self.gregorian isDateInToday:date]) {
return NO;
}else{
return YES;
}
}
- (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
return YES;
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSLog(@"did select date %@",[self.dateFormatter stringFromDate:date]);
if ([_dataArr containsObject:[self.dateFormatter stringFromDate:date]]) {
return;
}
if (![_dataArr containsObject:[self.dateFormatter stringFromDate:date]]) {
[calendar reloadData];
}else{
//重复的不加
}
}
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSLog(@"did deselect date %@",[self.dateFormatter stringFromDate:date]);
}
- (nullable UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleSelectionColorForDate:(NSDate *)date{
return [UIColor blackColor];
}
////点击今日签到的方法
//-(void)clickQiandao
//{
// if (![_dataArr containsObject:[self.dateFormatter stringFromDate:[NSDate date]]]) {
// [_dataArr addObject:[self.dateFormatter stringFromDate:[NSDate date]]];
//
// for (int i=0; i<_dataArr.count; i++) {
// [self.calendar selectDate:[self.dateFormatter dateFromString:_dataArr[i]] scrollToDate:YES];
// }
//
// [_calendar reloadData];
// }
//}
- (CGFloat)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderRadiusForDate:(nonnull NSDate *)date
{
return 0.0;
}
- (UIImage *)calendar:(FSCalendar *)calendar imageForDate:(NSDate *)date
{
//点击签到按钮
if ([self.gregorian isDateInToday:date]) {
UIImage * img = [UIImage imageNamed:@"勾16"];
return img;
}
else if ([_dataArr containsObject:[self.dateFormatter stringFromDate:date]]) {
return [UIImage imageNamed:@"勾16"];
// @"对勾"
}
else{
return nil;
}
}