思路 获取当前时间 向前向后计算出一个星期的时间放进数组,,右划时根据周日时间向后计算七天。。。左滑根据周一时间向前计算七天,,,底部是scrollview,添加三个View 每个View上七个Label , 循环思路是右划完成 第二个View回到中间 数据发生改变,,左划完成 第二个View仍回到中间 数据发生改变 给人的错觉是一直可以循环滑动
核心代码
- (void)createView{
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.titleLabel];
_nsdateArray = [NSMutableArray array];
self.dateArray = [NSMutableArray array];
//
self.titleLabel.text = [self getNowDate:[NSDate date]];
NSArray *weekArr = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
for (NSInteger i = 0; i<7; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*self.frame.size.width/7, 30, self.frame.size.width/7, 30)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:12];
label.text = weekArr[i];
[self addSubview:label];
}
self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60, self.frame.size.width, 40)];
self.backScrollView.contentSize = CGSizeMake(self.frame.size.width*3, 0);
[self addSubview:self.backScrollView];
self.backScrollView.delegate = self;
self.backScrollView.pagingEnabled = YES;
self.backScrollView.showsHorizontalScrollIndicator = NO;
self.dateViewArr = [NSMutableArray array];
///
NSInteger a = [self getDayChafromMonday:[self getNowWeek]];
NSInteger b = [self getDayChaToSunday:[self getNowWeek]];
/// 当前星期格式化时间
self.dateArray = [self getDateArrCarFro:a chaTo:b];
/// 当前星期标准时间
self.nsdateArray = [self getNsDateArrCarFro:a chaTo:b];
///// 一开始的显示不要调用leftButtonAction,rightButtonAction方法,防止左右滑时数据出错
for (NSInteger i = 0; i<3; i++) {
self.dateView = [[dateView alloc] initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, 40)];
[self.backScrollView addSubview:self.dateView];
[self.dateViewArr addObject:self.dateView];
if (i==0) {
NSMutableArray *dateArray = [NSMutableArray array];
NSMutableArray *nsdateArray = [NSMutableArray array];
NSDate *date = [self.nsdateArray firstObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:date];//前i天
[dateArray insertObject:[self getDateArr:lastDay] atIndex:0];
[nsdateArray insertObject:lastDay atIndex:0];
}
[self.dateView reloadButtonTitle:dateArray nsDateArr:nsdateArray];
}else if (i==1) {
NSInteger a = [self getDayChafromMonday:[self getNowWeek]];
NSInteger b = [self getDayChaToSunday:[self getNowWeek]];
self.dateArray = [self getDateArrCarFro:a chaTo:b];
[self.dateView reloadButtonTitle:self.dateArray nsDateArr:self.nsdateArray];
}else{
NSMutableArray *dateArray = [NSMutableArray array];
NSMutableArray *nsdateArray = [NSMutableArray array];
NSDate *date = [self.nsdateArray lastObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//后i天
[dateArray addObject:[self getDateArr:lastDay]];
[nsdateArray addObject:lastDay];
}
[self.dateView reloadButtonTitle:dateArray nsDateArr:nsdateArray];
}
}
[self setScrollViewContentOffsetCenter];
}
/// 偏移到中心位置
- (void)setScrollViewContentOffsetCenter {
[self.backScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int contentOffsetX = scrollView.contentOffset.x;
/// 右划
if(contentOffsetX >= (2 * self.frame.size.width)) {
/// 刷新中间页日期
[self rightScrollAction];
[self.dateViewArr[1] reloadButtonTitle:self.dateArray nsDateArr:self.nsdateArray];
/// 刷新后一页日期
NSMutableArray *dateArray = [NSMutableArray array];
NSMutableArray *nsdateArray = [NSMutableArray array];
NSDate *date = [self.nsdateArray lastObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//后i天
[dateArray addObject:[self getDateArr:lastDay]];
[nsdateArray addObject:lastDay];
}
[self.dateViewArr[2] reloadButtonTitle:dateArray nsDateArr:nsdateArray];
/// 刷新前一页日期
NSMutableArray *ldateArray = [NSMutableArray array];
NSMutableArray *lnsdateArray = [NSMutableArray array];
NSDate *ldate = [self.nsdateArray firstObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:ldate];//前i天
[ldateArray insertObject:[self getDateArr:lastDay] atIndex:0];
[lnsdateArray insertObject:lastDay atIndex:0];
}
[self.dateViewArr[0] reloadButtonTitle:ldateArray nsDateArr:lnsdateArray];
[self.backScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
}
/// 左滑
if(contentOffsetX <= 0) {
/// 刷新中间页日期
[self leftScrollAction];
[self.dateViewArr[1] reloadButtonTitle:self.dateArray nsDateArr:self.nsdateArray];
/// 刷新前一页日期
NSMutableArray *ldateArray = [NSMutableArray array];
NSMutableArray *lnsdateArray = [NSMutableArray array];
NSDate *ldate = [self.nsdateArray firstObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:ldate];//前i天
[ldateArray insertObject:[self getDateArr:lastDay] atIndex:0];
[lnsdateArray insertObject:lastDay atIndex:0];
}
[self.dateViewArr[0] reloadButtonTitle:ldateArray nsDateArr:lnsdateArray];
/// 刷新后一页日期
NSMutableArray *dateArray = [NSMutableArray array];
NSMutableArray *nsdateArray = [NSMutableArray array];
NSDate *date = [self.nsdateArray lastObject];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//后i天
[dateArray addObject:[self getDateArr:lastDay]];
[nsdateArray addObject:lastDay];
}
[self.dateViewArr[2] reloadButtonTitle:dateArray nsDateArr:nsdateArray];
[self.backScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
}
}
- (void)rightScrollAction{
NSDate *date = [self.nsdateArray lastObject];
[self.dateArray removeAllObjects];
[self.nsdateArray removeAllObjects];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//前i天
[self.dateArray addObject:[self getDateArr:lastDay]];
[self.nsdateArray addObject:lastDay];
}
}
- (void)leftScrollAction{
NSDate *date = [self.nsdateArray firstObject];
[self.dateArray removeAllObjects];
[self.nsdateArray removeAllObjects];
for (NSInteger i = 1; i<=7; i++) {
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:date];//前i天
[self.dateArray insertObject:[self getDateArr:lastDay] atIndex:0];
[self.nsdateArray insertObject:lastDay atIndex:0];
}
}
//// 获取当前这个星期格式化的时间
- (NSMutableArray *)getDateArrCarFro:(NSInteger)chafro chaTo:(NSInteger)chato{
NSMutableArray *dateArr = [NSMutableArray array];
for (NSInteger i = 1; i<=chafro; i++) {
NSDate * date = [NSDate date];//当前时间
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:date];//前i天
[dateArr insertObject:[self getDateArr:lastDay] atIndex:0];
}
[dateArr addObject:[self getDateArr:[NSDate date]]];
for (NSInteger i = 1; i<=chato; i++) {
NSDate * date = [NSDate date];//当前时间
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//后i天
[dateArr addObject:[self getDateArr:lastDay]];
}
return dateArr;
}
//// 获取当前这个个星期标准时间
- (NSMutableArray *)getNsDateArrCarFro:(NSInteger)chafro chaTo:(NSInteger)chato{
NSMutableArray *dateArr = [NSMutableArray array];
for (NSInteger i = 1; i<=chafro; i++) {
NSDate * date = [NSDate date];//当前时间
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:date];//前i天
[dateArr insertObject:lastDay atIndex:0];
}
[dateArr addObject:[NSDate date]];
for (NSInteger i = 1; i<=chato; i++) {
NSDate * date = [NSDate date];//当前时间
NSDate *lastDay = [NSDate dateWithTimeInterval:+24*60*60*i sinceDate:date];//后i天
[dateArr addObject:lastDay];
}
return dateArr;
}
//// 获取当前日期 显示为标题时间
- (NSString *)getNowDate:(NSDate *)senddate{
//获取系统当前的时间
NSCalendar *cal=[NSCalendar currentCalendar];
NSUInteger unitFlags= NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday;
NSDateComponents *conponent= [cal components:unitFlags fromDate:senddate];
//获取年、月、日
NSInteger year = [conponent year];
NSInteger month = [conponent month];
NSInteger day = [conponent day];
//周
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"EEEE"];
NSString *weekString = [dateformatter stringFromDate:senddate];
NSString *dateStr = [NSString stringWithFormat:@"%ld年%ld月%ld日 %@",year,month,day,[self getWeekChinese:weekString]];
return dateStr;
}
//根据传进来的date 返回一个由 年,月,日 星期组成的数组。。。。
- (NSArray *)getDateArr:(NSDate *)senddate{
NSCalendar *cal=[NSCalendar currentCalendar];
NSUInteger unitFlags= NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday;
NSDateComponents *conponent= [cal components:unitFlags fromDate:senddate];
//获取年、月、日
NSInteger year = [conponent year];
NSInteger month = [conponent month];
NSInteger day = [conponent day];
//周
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"EEEE"];
NSString *weekString = [dateformatter stringFromDate:senddate];
NSArray *arr = @[[NSString stringWithFormat:@"%ld",year],[NSString stringWithFormat:@"%ld",month],[NSString stringWithFormat:@"%ld",day],[self getWeekChinese:weekString]];
return arr;
}
/// 获取当前这天是星期几
- (NSString *)getNowWeek{
NSDate * senddate=[NSDate date];
//周
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"EEEE"];
NSString *weekString = [dateformatter stringFromDate:senddate];
return weekString;
}
demo
链接:https://pan.baidu.com/s/1QMZ7ZxkDaEHsUbejNdVh4w 密码:7atq