左右可以滑动半年的超级日历,支持日历部分收起和自动重定向为北京时间

简介: 左右可以滑动半年的超级日历,支持日历部分收起和自动重定向为北京时间

前后可以滑动半年的超级日历,支持日历部分收起和自动重定向为北京时间。

服务器返回的是北京时间,无论手机在那个时区或设置为那个时区,都重定向为北京时间,以保证日历的日期正确性。

BGCalendarEntity.m

#import "BGCalendarEntity.h"

@implementation BGCalendarEntity

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self fillData];
    }
    return self;
}

-(void)fillData
{
    self.list = [NSMutableArray array];
    self.shortList = [NSMutableArray array];
    self.localServerDifferenceTimeZone = 8*3600*1000 - [self getZoneTimeDifference];
}

-(BOOL)leapYear : (long)currentday
{
    if(currentday % 400 == 0)
    {
        return YES;
    }
    else if((currentday % 100 != 0) && (currentday % 4 == 0))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
#pragma mark - 把绝对时间转换为年/月/日格式的日期字符串
-(BGCalendarUnitEntity *)currentDateWithNowTime:(long long)nowTime
{
    if(nowTime < 0)
    {
        return @"未知时间";
    }

    long long  dateDistace = (nowTime/1000 + 8*3600)/86400;
    long long n = 0;
    long i,j,k,m;
    long month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    NSMutableDictionary *dic = [self getStartDayWithminDistace:dateDistace];
    i = [dic[@"startDay"] longValue];
    n = [dic[@"startValue"] longLongValue];
    for(j = 1, k = 1; (n <= dateDistace) && (i < 3000); i++)
    {
        for(j = 1; j <= 12 ; j++)
        {
            m = month[j - 1];
            for(k = 1; k <= m; k++)
            {
                n++;
                if(n == dateDistace)
                {
                    return  [NSString stringWithFormat:@"%ld/%ld/%ld", i, j,k];
                }
            }

            if((j == 2) && [ self leapYear: i])
            {
                n++;
                if(n == dateDistace)
                {
                    return  [NSString stringWithFormat:@"%ld/%ld/%ld", i, j,k];
                }
            }
        }
    }

    return  nil;
}
-(long long)getZoneTimeDifference
{
    NSDate *date = [NSDate date]; // 获得时间对象

    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区

    NSTimeInterval time = [zone secondsFromGMTForDate:date]*1000;// 以秒为单位返回当前时间与系统格林尼治时间的差
    return (long long)time;
}

-(long long)getNowTime
{
    long long nowTime = (long long)([[NSDate date] timeIntervalSince1970]*1000+[self getZoneTimeDifference])+self.localServerDifferenceTime + self.localServerDifferenceTimeZone;
    return nowTime;
}
-(long long)getStandardNowTime
{
    long long nowTime = (long long)([[NSDate date] timeIntervalSince1970]*1000+[self getZoneTimeDifference])+self.localServerDifferenceTime + self.localServerDifferenceTimeZone -8*3600*1000;
    return nowTime;
}

-(NSDate *)getDateNow
{
    NSDate *dateNow = [[NSDate date] dateByAddingTimeInterval:([self getZoneTimeDifference] + self.localServerDifferenceTime + self.localServerDifferenceTimeZone)/1000];// 然后把差的时间加上,就是当前系统准确的时间
    return dateNow;
}

-(NSDate *)getStandardDateNow
{
    NSDate *dateNow = [[NSDate date] dateByAddingTimeInterval:([self getZoneTimeDifference] + self.localServerDifferenceTime + self.localServerDifferenceTimeZone -8*3600*1000)/1000];//0时区的时间
    return dateNow;
}

-(NSDate *)getStandardDateNowWithLocalServerDifferenceTime:(long long)localServerDifferenceTime
{
    NSDate *dateNow = [[NSDate date] dateByAddingTimeInterval:([self getZoneTimeDifference] + localServerDifferenceTime + self.localServerDifferenceTimeZone -8*3600*1000)/1000];// 然后把差的时间加上,就是当前系统准确的时间
    return dateNow;
}

-(NSDate *)getStandardDateNowWithInterval:(long long)interval
{
    NSDate *dateNow = [[NSDate date] dateByAddingTimeInterval:([self getZoneTimeDifference] + self.localServerDifferenceTime + self.localServerDifferenceTimeZone -8*3600*1000)/1000+interval*24*3600];// 然后把差的时间加上,就是当前系统准确的时间
    return dateNow;
}

#pragma mark - 把绝对时间转换为今天,昨天,明天,周几
-(BOOL)resetCalendarDataWithLocalServerDifferenceTime:(long long)localServerDifferenceTime
{
    self.localServerDifferenceTime = localServerDifferenceTime;
    long long nowTime = [self getNowTimeWithLocalServerDifferenceTime:localServerDifferenceTime];// (long long)([[NSDate date] timeIntervalSince1970])+ 8*3600+localServerDifferenceTime/1000;
    nowTime = nowTime/1000;
    long long maxTime = 31*8*24*3600;
    maxTime = nowTime + maxTime;
    //注意Java服务器获取到的时间是精确到毫秒的,苹果获取到的时间默认是精确到秒,若想得到毫秒需要乘以1000,反之要除以1000;获取到的绝对时间都是以零时区的时间为准,所以北京要加八个小时时间
    long long  distace = 31*7*24*3600;//1*1*24*3600;
    long long  minDistace = (nowTime -distace)/86400;
    long long  maxDistace = (nowTime +distace)/86400;
    long long  nowDistace = (nowTime)/86400;

    long long n = 0;
    long long i,j,k,m,interval;
    interval = 0;
    long month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    NSMutableDictionary *dic = [self getStartDayWithminDistace:minDistace];
    i = [dic[@"startDay"] longValue];
    n = [dic[@"startValue"] longLongValue];
    long monthCount = 0;
    interval = 0;
    
    NSMutableArray *arr = [NSMutableArray array];
    NSArray *weekdays = [NSArray arrayWithObjects: @"一", @"二", @"三", @"四", @"五", @"六",@"日", nil];
    
    BGCalendarUnitEntity *minEntity = [self getWeekDatValueWithLocalServerDifferenceTime:localServerDifferenceTime interval:minDistace - nowDistace];
    self.minEntity = minEntity;
    if(minEntity)
    {
        [arr addObject:minEntity];
    }
    
    for(j = 1, k = 1; (n <= maxDistace) && (i < 5000); i++)
    {
        for(j = 1; j <= 12 ; j++)
        {
            monthCount++;
            m = month[j - 1];
            for(k = 1; k <= m; k++)
            {
//                if(1 == j && 1 == k)
//                {
//                    NSLog(@"n:%lld", n);
//                }
                if(n == maxDistace)
                {
                    return [self updateListWithArr:arr];//[self getWeekDatValueWithIsOverNow:isOverNow interval:interval];
                }
                else if(n == minDistace)
                {
                    interval++;
                    monthCount = 0;
                    minEntity.intervalGMT = n;
                }
                else if(interval > 0)
                {
                    BGCalendarUnitEntity *entity = [[BGCalendarUnitEntity alloc] init];
                    entity.intervalGMT = n;
                    entity.weekdayIndex = (minEntity.weekdayIndex + interval)%7;
                    entity.year = i;
                    entity.month = j;
                    entity.day = k;
                    entity.yearMonth = [NSString stringWithFormat:@"%lu.%lu月", entity.year, entity.month];
                    entity.value = [NSString stringWithFormat:@"%lu",  entity.day];
                    entity.weekName = [weekdays objectAtIndex:entity.weekdayIndex];
                    if(n == nowDistace)
                    {
                        entity.isShowToday = YES;
                        self.nowEntity = entity;
                        entity.isSelect = YES;
                        self.selectEntity = entity;
                    }
                    entity.timeStamp = minEntity.timeStamp + interval*24*3600*1000;
//                    entity.weekdayIndex = interval;
                    if(entity.day >= 1 && entity.day <= 7){
                        entity.row = 0;
                    }
                    else if(entity.day >= 8 && entity.day <= 14){
                        entity.row = 1;
                    }
                    else if(entity.day >= 15 && entity.day <= 21){
                        entity.row = 2;
                    }
                    else if(entity.day >= 22 && entity.day <= 28){
                        entity.row = 3;
                    }
                    else
                    {
                        entity.row = 4;
                    }
                    entity.monthCount = monthCount;
                    [arr addObject:entity];
                    interval++;
                }
                n++;
            }

            if((j == 2) && [ self leapYear: i])
            {
                if(n == maxDistace)
                {
                    return [self updateListWithArr:arr];//[self getWeekDatValueWithIsOverNow:isOverNow interval:interval];
                }
                else if(n == minDistace)
                {
                    interval++;
                    monthCount = 0;
                    minEntity.intervalGMT = n;
                }
                else if(interval > 0)
                {
                    interval++;
                    BGCalendarUnitEntity *entity = [[BGCalendarUnitEntity alloc] init];
                    entity.intervalGMT = n;
                    entity.weekdayIndex = (minEntity.weekdayIndex + interval)%7;
                    entity.year = i;
                    entity.month = j;
                    entity.day = k+1;
                    entity.yearMonth = [NSString stringWithFormat:@"%lu.%lu月", entity.year, entity.month];
                    entity.value = [NSString stringWithFormat:@"%lu",  entity.day];
                    entity.weekName = [weekdays objectAtIndex:entity.weekdayIndex];
                    entity.monthCount = monthCount;
                    if(n == nowDistace)
                    {
                        entity.isShowToday = YES;
                        self.nowEntity = entity;
                    }
                    entity.timeStamp = nowTime + (interval - distace/86400)*24*3600*1000;
                    if(entity.day >= 1 && entity.day <= 7){
                        entity.row = 0;
                    }
                    else if(entity.day >= 8 && entity.day <= 14){
                        entity.row = 1;
                    }
                    else if(entity.day >= 15 && entity.day <= 21){
                        entity.row = 2;
                    }
                    else if(entity.day >= 22 && entity.day <= 28){
                        entity.row = 3;
                    }
                    else
                    {
                        entity.row = 4;
                    }
                    [arr addObject:entity];
                }
                n++;
            }
        }
    }

    return  NO;
}

-(BOOL)updateListWithArr:(NSMutableArray *)arr
{
    if(!self.nowEntity || ![self.nowEntity isKindOfClass:[BGCalendarUnitEntity class]] || !arr || ![arr isKindOfClass:[NSArray class]] || arr.count == 0)
    {
        return NO;
    }
    
    
    NSMutableArray *tempArr = nil;
//    NSMutableArray *tempList = [NSMutableArray array];
    NSInteger n = self.nowEntity.monthCount + 6;
    NSInteger m = self.nowEntity.monthCount - 6;
    long long tempMonthCount = 0;
    long long newMonthCount = 0;
    [self.list removeAllObjects];
    [self.shortList removeAllObjects];
    for(NSInteger i = 0; i < arr.count; i++)
    {
        BGCalendarUnitEntity *entity = arr[i];
        if(entity.monthCount >= m  && entity.monthCount <= n)
        {
            if(entity.monthCount != tempMonthCount)
            {
                if(tempArr)
                {
                    [self.list addObject:tempArr];
                    newMonthCount++;
                }
                tempArr = [NSMutableArray array];
                [tempArr addObject:entity];
            }
            else
            {
                [tempArr addObject:entity];
            }
            tempMonthCount = entity.monthCount;
            entity.monthCount = newMonthCount;
        }
    }
    [arr removeAllObjects];
    if(tempArr)
    {
        [self.list addObject:tempArr];
        for(NSMutableArray *arr in self.list)
        {
            if(arr && [arr isKindOfClass:[NSArray class]] && arr.count > 0)
            {
                BGCalendarUnitEntity *entity = arr[0];
                for(NSInteger i = 0; i < 7 && i < entity.weekdayIndex; i++)
                {
                    BGCalendarUnitEntity *emptyEntity = [[BGCalendarUnitEntity alloc] init];
                    [arr insertObject:emptyEntity atIndex:0];
                }
            }
        }
        self.currentSection = 6;
    }
    else
    {
        return NO;
    }

//    BGCalendarUnitEntity *entity = nil;
//    NSMutableArray *monthArr = nil;
//    for(NSInteger i = 0; i < tempList.count; i++)
//    {
//        monthArr = tempList[i];
//        for(NSInteger j = 0; j < 35; j++)
//        {
//            if(j < 7 && j < monthArr.count)
//            {
//                entity = monthArr[j];
//                [self.shortList addObject:entity];
//                [self.list addObject:entity];
//            }
//            else if(j < monthArr.count)
//            {
//                entity = monthArr[j];
//                [self.list addObject:entity];
//            }
//            else
//            {
//                entity = [[BGCalendarUnitEntity alloc] init];
//                [self.list addObject:entity];
//            }
//        }
//    }
//    [tempList removeAllObjects];
//    [arr removeAllObjects];
    return YES;
}

-(NSMutableDictionary *)getStartDayWithminDistace:(long long)minDistace
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if(minDistace >= 18396)
    {
        [dic setSafeObject:@"2020" forKey:@"startDay"];
        [dic setSafeObject:@"18262" forKey:@"startValue"];
    }
    else if(minDistace >= 17167)
    {
        [dic setSafeObject:@"2017" forKey:@"startDay"];
        [dic setSafeObject:@"17167" forKey:@"startValue"];
    }
    else if(minDistace >= 16436)
    {
        [dic setSafeObject:@"2015" forKey:@"startDay"];
        [dic setSafeObject:@"16436" forKey:@"startValue"];
    }
    else if(minDistace >= 14610)
    {
        [dic setSafeObject:@"2010" forKey:@"startDay"];
        [dic setSafeObject:@"14610" forKey:@"startValue"];
    }
    else if(minDistace >= 10957)
    {
        [dic setSafeObject:@"2000" forKey:@"startDay"];
        [dic setSafeObject:@"10957" forKey:@"startValue"];
    }
    else
    {
        [dic setSafeObject:@"1970" forKey:@"startDay"];
        [dic setSafeObject:@"0" forKey:@"startValue"];
    }
    return dic;
}

-(long long)getNowTimeWithLocalServerDifferenceTime:(long long)localServerDifferenceTime
{
    long long nowTime = (long long)([[NSDate date] timeIntervalSince1970]*1000+[self getZoneTimeDifference])+localServerDifferenceTime;
    return nowTime;
}

-(NSDate *)getDateNowWithLocalServerDifferenceTime:(long long)localServerDifferenceTime
{
    NSDate *dateNow = [[NSDate date] dateByAddingTimeInterval:([self getZoneTimeDifference] + localServerDifferenceTime/1000)];// 然后把差的时间加上,就是当前系统准确的时间
    return dateNow;
}

-(BGCalendarUnitEntity *)getWeekDatValueWithLocalServerDifferenceTime:(long long)localServerDifferenceTime
                                                             interval:(long long)interval
{
//    long long nowTime = [self getNowTimeWithLocalServerDifferenceTime:localServerDifferenceTime];
    NSDate *inputDate = [self getStandardDateNowWithInterval:interval];
    
//    long long nowTime = (long long)([[NSDate date] timeIntervalSince1970]+time)+localServerDifferenceTime/1000;
//    NSDate *inputDate = [NSDate dateWithTimeIntervalSince1970:nowTime];
    NSArray *weekdays = [NSArray arrayWithObjects: @"一", @"二", @"三", @"四", @"五", @"六",@"日", nil];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
//    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];

    [calendar setTimeZone: timeZone];

    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    /*注意经过测试,就是你把这个日期(inputDate)置为以前的时间,通过[calendar components:calendarUnit fromDate:inputDate]你得到的theComponents.weekday也是今天所的周几,如:今天是周五,theComponents.weekday为5,和inputDate无关。所以只有你自己实现和今天的差的天数来确定具体哪天是周几才正确*/
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    NSDateComponents *comps = [calendar components:(NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute)  fromDate:inputDate];
    NSInteger weekdayIndex = theComponents.weekday -2;
    if(weekdayIndex < 0)
    {
        weekdayIndex = 6;
    }
//    NSInteger currenWeekdayIndex = weekdayIndex;
//    long long num = (interval)%7;
//    weekdayIndex = (num+7+weekdayIndex)%7;
    NSLog(@"inputDate:%@, comps:%@,interval:%lld, weekdayIndex = %d, week:%@", inputDate, comps, interval, weekdayIndex,[weekdays objectAtIndex:weekdayIndex]);
//    self.currenWeekdayIndex = currenWeekdayIndex;
    BGCalendarUnitEntity *entity = [[BGCalendarUnitEntity alloc] init];
    entity.weekdayIndex = weekdayIndex;
    entity.year = comps.year;
    entity.month = comps.month;
    entity.day = comps.day;
//    entity.currenWeekdayIndex = currenWeekdayIndex;
    entity.yearMonth = [NSString stringWithFormat:@"%lu.%lu月", entity.year, entity.month];
    entity.value = [NSString stringWithFormat:@"%lu",  entity.day];
    entity.weekName = [weekdays objectAtIndex:weekdayIndex];
    entity.isShowToday = NO;
    long long nowTime = [self getNowTimeWithLocalServerDifferenceTime:localServerDifferenceTime];
    entity.timeStamp = nowTime + interval*24*3600*1000;
    entity.monthCount = 0;
//    entity.weekdayIndex = interval;
    if(entity.day >= 1 && entity.day <= 7){
        entity.row = 0;
    }
    else if(entity.day >= 8 && entity.day <= 14){
        entity.row = 1;
    }
    else if(entity.day >= 15 && entity.day <= 21){
        entity.row = 2;
    }
    else if(entity.day >= 22 && entity.day <= 28){
        entity.row = 3;
    }
    else
    {
        entity.row = 4;
    }
    return entity;//[weekdays objectAtIndex:weekdayIndex];
}

@end

BGCalendarEntity.h

#import <Foundation/Foundation.h>
#import "BGCalendarUnitEntity.h"

@interface BGCalendarEntity : NSObject
@property (nonatomic, strong) NSMutableArray *shortList;
@property (nonatomic, strong) NSMutableArray *list;

@property(nonatomic, assign) long long localServerDifferenceTime;
@property(nonatomic, assign) long long localServerDifferenceTimeZone;
@property(nonatomic, strong) BGCalendarUnitEntity *minEntity ;
@property(nonatomic, strong) BGCalendarUnitEntity *nowEntity;
@property(nonatomic, strong) BGCalendarUnitEntity *selectEntity;
@property(nonatomic, assign) long long currentSection;



- (BGCalendarUnitEntity *)currentDateWithNowTime:(long long)nowTime;
- (BOOL)resetCalendarDataWithLocalServerDifferenceTime:(long long)localServerDifferenceTime;
@end

BGCateToolView.h

#import <UIKit/UIKit.h>
//#import "BGCalendarHeadView.h"
#import "BGCateToolEntity.h"

@interface BGCateToolView : BGBaseView
//@property (nonatomic, strong) BGCalendarHeadView *calendarHeadView;
//
@property (nonatomic, strong) BGCateToolUnitEntity *selectCateToolUnitEntity;
//@property (nonatomic, assign) BOOL isShowToday;
//@property (nonatomic, assign) BOOL isUnfold;
@property (nonatomic, strong) BGCateToolEntity *model;

//@property (nonatomic, copy) void(^hitBlock)(NSUInteger year, NSUInteger month, NSUInteger day);
@property (nonatomic, copy) void (^selectBlock)(BGCateToolUnitEntity *selectCateToolUnitEntity);

//- (void)updateWithSelectCateToolUnitEntity:(BGCateToolUnitEntity *)selectCateToolUnitEntity model:(BGCateToolEntity *)model;
@end

BGCateToolView.m

#import "BGCateToolView.h"
#import "BGCalendarFootView.h"
#import "BGCateToolCollectionCell.h"

@interface BGCateToolView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>
//@property (nonatomic, assign) BOOL isEnabledSelect;
//@property (nonatomic, assign) BOOL isHiddenLine;
//@property (nonatomic, strong) UIColor *selectColor;
//@property (nonatomic, strong) UIView *roundView;
@property (nonatomic, strong) UIView *bigBackgroundView;
//@property (nonatomic, strong) UIColor *lineColor;
//@property (nonatomic, strong) UIImage *icon;
//@property (nonatomic, strong) UIImage *selectedIcon;
//
//
//
//@property (nonatomic, strong) BGCalendarFootView *calendarFootView;
@property (nonatomic, strong) UICollectionView *collectionView;
//@property (nonatomic, strong) UICollectionReusableView *headView;
//@property (nonatomic, strong) UICollectionReusableView *collectionHeadView;
//@property (nonatomic, strong) UICollectionReusableView *parentCoursesCollectionHeadView;
//@property (nonatomic, strong) UICollectionReusableView *footmarkHeadView;
//@property (nonatomic, strong) UICollectionReusableView *lineFootView;
//
//@property (nonatomic,strong) UIPanGestureRecognizer *painRecognize1;
//@property (nonatomic, assign) float shift;
//@property (nonatomic, assign) BOOL isPanGestureRecognizer;
@end

@implementation BGCateToolView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup
{
    self.backgroundColor = [UIColor clearColor];
    self.bigBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sCommonUnitFullWidth(), 20+14+2+4+15)];
    self.bigBackgroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bigBackgroundView];
    [self.bigBackgroundView addSubview:self.collectionView];
}


- (UICollectionView *)collectionView {
    if(!_collectionView)
    {
        //创建布局
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        //创建CollectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(15, 20, sCommonUnitFullWidth()-15, 14+2+4) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = NO;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor whiteColor];//RGBA(246, 246, 246, 1);//BGColorHex(F9F9F9);
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"lineFootView"];
        [_collectionView registerClass:[BGCateToolCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([BGCateToolCollectionCell class])];

        //定义每个UICollectionView 的大小
        flowLayout.itemSize = CGSizeMake(80 , 20);
        //定义每个UICollectionView 横向的间距
        flowLayout.minimumLineSpacing = BG_1PX;
        //定义每个UICollectionView 纵向的间距
        flowLayout.minimumInteritemSpacing = BG_1PX;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        //定义每个UICollectionView 的边距距
        //    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//上左下
    }
    return _collectionView;
}

//两个cell之间的间距(同一行的cell的间距)

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return BG_1PX;
}

//这个是两行cell之间的间距(上下行cell的间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return BG_1PX;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BGCateToolCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BGCateToolCollectionCell class]) forIndexPath:indexPath];
    [cell updateWithSelectCateToolUnitEntity:self.selectCateToolUnitEntity model:[self.model.cate_list bitobjectOrNilAtIndex:indexPath.row]];
    return  cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.cate_list.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return isCommonUnitEmptyArray(self.model.cate_list) ? 0 : self.model.cate_list.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectCateToolUnitEntity = [self.model.cate_list bitobjectOrNilAtIndex:indexPath.row];
    if(self.selectCateToolUnitEntity && [self.selectCateToolUnitEntity isKindOfClass:[BGCateToolUnitEntity class]] && self.selectBlock)
    {
        self.selectBlock(self.selectCateToolUnitEntity);
        [self.collectionView reloadData];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(80, 20);
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(FULL_WIDTH, BG_1PX);
//}


创建头视图
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if (self.lineFootView)
//    {
//        return self.lineFootView;
//    }
//    
//    UICollectionReusableView *lineFootView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
//                                                                                withReuseIdentifier:@"lineFootView" forIndexPath:indexPath];
//    self.lineFootView = lineFootView;
//    UIView *lineFootViewPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, BG_1PX)];
//    lineFootViewPanel.backgroundColor = [UIColor clearColor];
//    [lineFootView addSubview:lineFootViewPanel];
//    return lineFootView;
//}


//- (BGCateToolEntity *)model {
//    if(!_model)
//    {
//        _model = [[BGCateToolEntity alloc] init];
//    }
//    return _model;
//}

//- (void)updateWithSelectCateToolUnitEntity:(BGCateToolUnitEntity *)selectCateToolUnitEntity model:(BGCateToolEntity *)model
//{
//    self.selectCateToolUnitEntity = selectCateToolUnitEntity;
//    self.model = model;
//}

-(void)setModel:(BGCateToolEntity *)model
{
    if(!model || ![model isKindOfClass:[BGCateToolEntity class]])
    {
        return;
    }
    _model = model;
    self.selectCateToolUnitEntity = [self.model.cate_list bitobjectOrNilAtIndex:0];
    [self.collectionView reloadData];
}

@end

BGCateToolCollectionCell.h

#import <UIKit/UIKit.h>
#import "PPCollectionViewCell.h"
//#import "BGCalendarMacro.h"
//#import "BGCalendarEntity.h"
#import "BGCateToolUnitEntity.h"

@interface BGCateToolCollectionCell : PPCollectionViewCell
@property (nonatomic, strong) UILabel *describeTitleLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) BGCateToolUnitEntity *model;
@property (nonatomic, assign) BOOL isHiddenLine;

- (instancetype)initWithFrame:(CGRect)frame;

- (void)updateWithSelectCateToolUnitEntity:(BGCateToolUnitEntity *)selectCateToolUnitEntity model:(BGCateToolUnitEntity *)model;

@end

BGCateToolCollectionCell.m

#import "BGCateToolCollectionCell.h"

@interface BGCateToolCollectionCell ()
@property (nonatomic, strong) UIView *bigBackgroundView;

@end

@implementation BGCateToolCollectionCell

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViewWithFrame:frame];
    }
    
    return  self;
    
}

-(void)setupViewWithFrame:(CGRect)frame
{
    
    self.bigBackgroundView = [[UIView alloc] init];
    self.bigBackgroundView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bigBackgroundView];
    
    [self.bigBackgroundView addSubview:self.describeTitleLabel];
    [self.bigBackgroundView addSubview:self.lineView];

    [self unitsSdLayout];
}

- (void)updateWithSelectCateToolUnitEntity:(BGCateToolUnitEntity *)selectCateToolUnitEntity model:(BGCateToolUnitEntity *)model
{
    if((!selectCateToolUnitEntity || ![selectCateToolUnitEntity isKindOfClass:[BGCateToolUnitEntity class]]) || (!model || ![model isKindOfClass:[BGCateToolUnitEntity class]]) || (selectCateToolUnitEntity != model) || isCommonUnitEmptyString(selectCateToolUnitEntity.cate_id))
    {
        self.isHiddenLine = YES;
        self.model = model;
    }
    else
    {
        self.isHiddenLine = NO;
        self.model = model;
    }
}


-(void)setIsHiddenLine:(BOOL)isHiddenLine
{
    self.lineView.hidden = isHiddenLine;
    _isHiddenLine = isHiddenLine;
}

-(void)setModel:(BGCateToolUnitEntity *)model
{
    if(!model || ![model isKindOfClass:[BGCateToolUnitEntity class]])
    {
        return;
    }
    self.describeTitleLabel.text = getNotNilString(model.name);
    [self.describeTitleLabel updateLayout];
    if(!self.isHiddenLine)
    {
        [self.lineView updateLayout];
    }
    _model = model;
}


-(void)unitsSdLayout
{
    self.bigBackgroundView.sd_layout
    .topSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0)
    .widthIs(self.frame.size.width)
    .heightIs(self.frame.size.height);
    
    self.describeTitleLabel.sd_layout
    .topSpaceToView(self.bigBackgroundView, 0)
    .leftSpaceToView(self.bigBackgroundView, 0)
    .heightIs(14);
    [self.describeTitleLabel setSingleLineAutoResizeWithMaxWidth:80];
    
    self.lineView.sd_layout
    .topSpaceToView(self.describeTitleLabel,  4)
    .centerXEqualToView(self.describeTitleLabel)
    .widthIs(40)
    .heightIs(2);
}


- (UIView *)lineView {
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = BGColorHex(FF7648);
        _lineView.hidden = YES;
    }
    return _lineView;
}

- (UILabel *)describeTitleLabel {

    if(!_describeTitleLabel)
    {
        _describeTitleLabel = [[UILabel alloc] init];
        _describeTitleLabel.backgroundColor = [UIColor whiteColor];
        _describeTitleLabel.textAlignment = NSTextAlignmentLeft;
        _describeTitleLabel.font = BGFont(14);
        _describeTitleLabel.numberOfLines = 1;
        _describeTitleLabel.textColor = COMMON_FONT_COLOR;
        _describeTitleLabel.text = @"教学用品";
    }

    return _describeTitleLabel;
}



@end

BGCalendarMacro.h


#ifndef BGCalendar_Macro_h
#define BGCalendar_Macro_h


#define sCalendarCellInterval  7.5


#endif

BGCalendarUnitEntity.h

#import <Foundation/Foundation.h>

@interface BGCalendarUnitEntity : BGBaseEntity
//@property (nonatomic, assign) NSInteger section;
@property (nonatomic, assign) NSInteger row;
@property (nonatomic, assign) BOOL isSelect;

@property (nonatomic, strong) NSString *value;
@property (nonatomic, strong) NSString *weekName;
@property (nonatomic, assign) long long weekdayIndex;
@property (nonatomic, assign) long long timeStamp;
@property (nonatomic, assign) BOOL isShowToday;

@property (nonatomic, assign) NSUInteger year;
@property (nonatomic, assign) NSUInteger month;
@property (nonatomic, assign) NSUInteger day;
@property (nonatomic, strong) NSString *yearMonth;

@property (nonatomic, assign) long long intervalGMT;

@property (nonatomic, assign) long long monthCount;

@end

#import “BGCalendarUnitEntity.h”

#import "BGCalendarUnitEntity.h"

@implementation BGCalendarUnitEntity

- (id)init
{
    if (self)
    {
        self.value = nil;
    }
    return self;
}

-(void)fillData
{

}


@end


目录
相关文章
SwiftUI—方便用户选择日期的DatePicker日期拾取器
SwiftUI—方便用户选择日期的DatePicker日期拾取器
1662 0
SwiftUI—方便用户选择日期的DatePicker日期拾取器
lunar="false",日历插件不显示农历
lunar="false",日历插件不显示农历
93 1
|
7月前
|
JavaScript 前端开发 容器
js使用表格生成一个日历,点击按钮上查看上个月,点击按钮下,查看下个月,月份,年份
js使用表格生成一个日历,点击按钮上查看上个月,点击按钮下,查看下个月,月份,年份
152 0
日历控件input框默认显示当日日期
日历控件input框默认显示当日日期
81 0
uniapp写一个随时间变化的预约日期列表
const today = new Date(); const dateArray = []; const dayOfWeekArray = []; const yearMonthDayArray = [];
95 0
|
前端开发 JavaScript 容器
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
361 0
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
|
JavaScript 前端开发 UED
【组件封装】显示实时时间和星期几·附文字特效
【组件封装】显示实时时间和星期几·附文字特效
【组件封装】显示实时时间和星期几·附文字特效
关于调起系统日历预填信息问题
最近开发遇到一个问题,需要调起系统日历,添加日历事件,但是会出现有些手机无法预填信息的情况;在这里对这个问题做个小记录;
131 0
关于调起系统日历预填信息问题
|
JavaScript 前端开发
倒计时跳转和获取实时时间
本文内容如题,自己做的一个demo,倒计时和获取实时时间在许多场景都用的到,所以还算蛮实用的,需要的朋友可以做个参考。 代码解析:html代码 <body onload="startTime()"> <div class="box"> <div class="time">请等待<span id="dd">6</span>秒</div> <!--设置时间长一点,不然一直跳转很烦--> ![](aaa.png) <div class="id-box"> <div id="time"></div>
295 0
倒计时跳转和获取实时时间