ios 在FSCalendar基礎上開發的簽到功能 記錄一下 不是太完善
#import "SignViewController.h"
#import <FSCalendarExtensions.h>
#import "XMGContent.h"
#import "AFNetworking.h"
#import "SignModel.h"
#import"UIView+MJAlertView.h"
#import <MJExtension.h>
@interface SignViewController ()
@end
@implementation SignViewController
//日期轉換後的集合
NSMutableArray<NSDate*>* signDatas;
//日期物件
NSMutableArray* signDatasStr;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title=@"簽到";
[self.Sing_Button addTarget:self action:@selector(goSign:) forControlEvents:UIControlEventTouchUpInside];
//讓CalendarParent接受使用者的手勢處理
[self.CalendarParent setUserInteractionEnabled:YES];
//日曆的設定
FSCalendar* uicalender=[[FSCalendar alloc]initWithFrame:CGRectMake(0, 0, _CalendarParent.frame.size.width, _CalendarParent.frame.size.height)];
[_CalendarParent addSubview:uicalender];
uicalender.delegate=self;
uicalender.dataSource=
self.calendar=uicalender;
self.calendar.appearance.caseOptions=FSCalendarCaseOptionsHeaderUsesDefaultCase;
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設定為中文
self.calendar.locale = locale;
self.calendar.appearance.headerMinimumDissolvedAlpha = 0;
_calendar.appearance.headerDateFormat = @"yyyy年MM月";
//初始話是否提交過簽到;
_sginFlag=false;
//呼叫聯網
[self getSingleData:[self getMonthFirstAndLastDayWith:[self getDataStr]]];
}
//聯網獲取簽到的資訊
-(void)getSingleData:(NSArray*)SectionDate{
NSString* url=[XMGBaseUrl stringByAppendingString:XMGGetSignData];
NSMutableDictionary* nmd=[NSMutableDictionary dictionary];
nmd[@"userid"]=@"5";
nmd[@"page"]=@"0";
nmd[@"rows"]=@"0";
//起始時間
NSLog(@"%@ 開始時間",SectionDate[0]);
NSLog(@"%@ 結束時間",SectionDate[1]);
nmd[@"time1"]=SectionDate[0];
//結束時間
nmd[@"time2"]=SectionDate[1];
[signDatasStrremoveAllObjects];
[signDatasremoveAllObjects];
[[AFHTTPSessionManagermanager]GET:[NSURLURLWithString:url].absoluteStringparameters:nmd progress:nilsuccess:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
signDatasStr=[SignModelmj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
signDatas=[[NSMutableArrayalloc]init];
for (int i=0; i<signDatasStr.count; i++) {
[signDatasaddObject:[selfchangeStrNsData:((SignModel*)signDatasStr[i]).date]];
}
[self.calendarreloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"聯網失敗");
}];
NSLog(@"我要重新整理了");
}
//標記已簽到的日期
//設定時間的選中狀態
-(BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
if ([selfisSameDay:date date2:[selfgetData]]) {
returntrue;
}
else{
returnfalse;
}
}
//點選簽到
-(void)goSign:(UIButton*)bt{
if (bt.tag==10001) {
[UIView addMJNotifierWithText:@"您已經簽到過了,明天再來吧" dismissAutomatically:YES AlterViewbackground:[UIColor greenColor] AlterViewtextColor:[UIColor grayColor]];
}
elseif (bt.tag==10002)
{
[self commitSignDate];
}
}
//聯網提交簽到日期
-(void)commitSignDate{
NSString* url=[XMGBaseUrl stringByAppendingString:XMGSignData];
NSMutableDictionary* nmud2=[NSMutableDictionary dictionary];
nmud2[@"userid"]=@"5";
[[AFHTTPSessionManager manager]GET:[NSURL URLWithString:url].absoluteString parameters:nmud2 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
[self getSingleData:[self getMonthFirstAndLastDayWith:[self getDataStr]]];
[UIView addMJNotifierWithText:@"簽到成功" dismissAutomatically:YES AlterViewbackground:[UIColor greenColor] AlterViewtextColor:[UIColor grayColor]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"提價失敗");
}];
}
//通過後臺簽到確定ui
-(NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date{
NSDate* today=[self getData];
NSString* result=nil;
for (NSDate* selectData in signDatas) {
int differs=[self getDifferDate:today and:selectData];
if ([self isSameDay:date date2:today]) {
if ([self isSameDay:selectData date2:date]) {
result=@"已簽到";
_sginFlag=10001;
}
else{
_sginFlag=10002;
result=@"今天";
}
}
elseif(differs>=0){
if ([self isSameDay:selectData date2:date]) {
result=@"已簽到";
}
}
else{
result= @"nil";
}
}
[_Sing_Button setTag:_sginFlag];
return result;
}
-(int)getDifferDate:(NSDate*)date1 and :(NSDate*)date2{
NSTimeInterval time = [date1 timeIntervalSinceDate:date2];
//開始時間和結束時間的中間相差的時間
int days;
days = ((int)time)/(3600*24);//一天是24小時*3600秒
return days;
}
//兩個日期判斷是否是同一天
- (BOOL)isSameDay:(NSDate*)date1 date2:(NSDate*)date2
{
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year]== [comp2 year];
}
//測試相減一天的處理
- (BOOL)isSameNoDay:(NSDate*)date1 date2:(NSDate*)date2 dats:(int)differ
{
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day]+differ == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year]== [comp2 year];
}
//頁面切換的時候
-(void)calendarCurrentMonthDidChange:(FSCalendar *)calendar{
NSDate* data=[self.calendar currentPage];
//NSDate *nextMonth = [self.calendar dateByAddingMonths:1 toDate:data];
NSArray* sectionDateN=[self getMonthFirstAndLastDayWith:[self getDataStrByDate:data]];
[self getSingleData:sectionDateN];
}
//通過指定日期獲取當月月初和月末的兩天
- (NSArray *)getMonthFirstAndLastDayWith:(NSString *)dateStr{
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *newDate=[format dateFromString:dateStr];
double interval = 0;
NSDate *firstDate = nil;
NSDate *lastDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
if (OK) {
lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
}else {
return@[@"",@""];
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[myDateFormatter setTimeZone:timeZone];
NSString *firstString = [myDateFormatter stringFromDate: firstDate];
NSString *lastString = [myDateFormatter stringFromDate: lastDate];
return@[firstString, lastString];
}
//獲取當前日期的字串
-(NSString*)getDataStr{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
return [formatter stringFromDate:date];
}
//獲取指定日期的字串
-(NSString*)getDataStrByDate:(NSDate*)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
return [formatter stringFromDate:date];
}
//獲取日期
-(NSDate*)getData{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
return date;
}
//字串轉日期格式
-(NSDate*)changeStrNsData:(NSString*)str{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
// 設定日期格式為了轉換成功
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[format setTimeZone:timeZone];
// NSString * -> NSDate *
NSDate *data = [format dateFromString:str];
return data;
}
//遮蔽不可點
-(BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date{
returnNO;
}
-(UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleSelectionColorForDate:(NSDate *)date{
if ([self.calendar isDateInToday:date]) {
return [UIColor blueColor];
}
returnnil;
}
@end