如何在两周模式下实现日历视图(使用 JTCalendar 示例)
how to implement Calendar view in two weeks mode(Used JTCalendar example)
我已经实现了 JTCalendar 视图以获取月视图和周视图,但在周视图中,我需要获取两周视图。建议我编码。
JT日历代码:
CGFloat x = 0;
CGFloat width = self.frame.size.width / 7.;
CGFloat height = self.frame.size.height;
if(self.calendarManager.calendarAppearance.readFromRightToLeft)
{
for(UIView *view in [[self.subviews reverseObjectEnumerator] allObjects])
{
view.frame = CGRectMake(x, 0, width, height);
x = CGRectGetMaxX(view.frame);
}
}
else{
for(UIView *view in self.subviews)
{
view.frame = CGRectMake(x, 0, width, height);
x = CGRectGetMaxX(view.frame);
}
}
[super layoutSubviews];
#//显示周数
- (void)configureConstraintsForSubviews
{
CGFloat weeksToDisplay;
if(cacheLastWeekMode)
{
weeksToDisplay = 1 ;
}
else{
weeksToDisplay = (CGFloat)(WEEKS_TO_DISPLAY + 1); // + 1 for weekDays
}
CGFloat y = 0;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height / weeksToDisplay;
for(int i = 0; i < self.subviews.count; ++i){
UIView *view = self.subviews[i];
view.frame = CGRectMake(0, y, width, height);
y = CGRectGetMaxY(view.frame);
if(cacheLastWeekMode && i == weeksToDisplay + 1){
height = 0.;
}
}
}
更改要显示的周数。
if(cacheLastWeekMode)
{
weeksToDisplay = 3 ;
}
在 MonthView 中更改周显示为 3
还要在委托方法 setBeginingofmonth
中注释以下代码
for(JTCalendarWeekView *view in weeksViews)
{
view.currentMonthIndex = currentMonthIndex;
[view setBeginningOfWeek:currentDate];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 7;
currentDate = [calendar dateByAddingComponents:dayComponent toDate:currentDate options:0];
// Doesn't need to do other weeks
// if(self.calendarManager.calendarAppearance.isWeekMode)
// {
// break;
// }
}
在 JTCalendar updatePage 方法中将 7 更改为 14,如下所示
if(!self.calendarAppearance.isWeekMode)
{
dayComponent.month = currentPage - (NUMBER_PAGES_LOADED / 2);
}
else
{
//to show next starting week for two weeks mode put 14
dayComponent.day = 14 * (currentPage - (NUMBER_PAGES_LOADED / 2));
}
- (void)setBeginningOfMonth:(NSDate *)date
{
NSDate *currentDate = date;
NSCalendar *calendar = self.calendarManager.calendarAppearance.calendar;
{
NSDateComponents *comps = [calendar components:NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
currentMonthIndex = comps.month;
// Hack
if(comps.day >7)
{
currentMonthIndex = (currentMonthIndex % 12) + 1;
}
}
for(JTCalendarWeekView *view in weeksViews)
{
view.currentMonthIndex = currentMonthIndex;
[view setBeginningOfWeek:currentDate];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 7;
currentDate = [calendar dateByAddingComponents:dayComponent toDate:currentDate options:0];
// Doesn't need to do other weeks
// if(self.calendarManager.calendarAppearance.isWeekMode)
// {
// break;
// }
}
}
JT日历代码:
CGFloat x = 0;
CGFloat width = self.frame.size.width / 7.;
CGFloat height = self.frame.size.height;
if(self.calendarManager.calendarAppearance.readFromRightToLeft)
{
for(UIView *view in [[self.subviews reverseObjectEnumerator] allObjects])
{
view.frame = CGRectMake(x, 0, width, height);
x = CGRectGetMaxX(view.frame);
}
}
else{
for(UIView *view in self.subviews)
{
view.frame = CGRectMake(x, 0, width, height);
x = CGRectGetMaxX(view.frame);
}
}
[super layoutSubviews];
#//显示周数
- (void)configureConstraintsForSubviews
{
CGFloat weeksToDisplay;
if(cacheLastWeekMode)
{
weeksToDisplay = 1 ;
}
else{
weeksToDisplay = (CGFloat)(WEEKS_TO_DISPLAY + 1); // + 1 for weekDays
}
CGFloat y = 0;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height / weeksToDisplay;
for(int i = 0; i < self.subviews.count; ++i){
UIView *view = self.subviews[i];
view.frame = CGRectMake(0, y, width, height);
y = CGRectGetMaxY(view.frame);
if(cacheLastWeekMode && i == weeksToDisplay + 1){
height = 0.;
}
}
}
更改要显示的周数。
if(cacheLastWeekMode)
{
weeksToDisplay = 3 ;
}
在 MonthView 中更改周显示为 3 还要在委托方法 setBeginingofmonth
中注释以下代码 for(JTCalendarWeekView *view in weeksViews)
{
view.currentMonthIndex = currentMonthIndex;
[view setBeginningOfWeek:currentDate];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 7;
currentDate = [calendar dateByAddingComponents:dayComponent toDate:currentDate options:0];
// Doesn't need to do other weeks
// if(self.calendarManager.calendarAppearance.isWeekMode)
// {
// break;
// }
}
在 JTCalendar updatePage 方法中将 7 更改为 14,如下所示
if(!self.calendarAppearance.isWeekMode)
{
dayComponent.month = currentPage - (NUMBER_PAGES_LOADED / 2);
}
else
{
//to show next starting week for two weeks mode put 14
dayComponent.day = 14 * (currentPage - (NUMBER_PAGES_LOADED / 2));
}
- (void)setBeginningOfMonth:(NSDate *)date
{
NSDate *currentDate = date;
NSCalendar *calendar = self.calendarManager.calendarAppearance.calendar;
{
NSDateComponents *comps = [calendar components:NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
currentMonthIndex = comps.month;
// Hack
if(comps.day >7)
{
currentMonthIndex = (currentMonthIndex % 12) + 1;
}
}
for(JTCalendarWeekView *view in weeksViews)
{
view.currentMonthIndex = currentMonthIndex;
[view setBeginningOfWeek:currentDate];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 7;
currentDate = [calendar dateByAddingComponents:dayComponent toDate:currentDate options:0];
// Doesn't need to do other weeks
// if(self.calendarManager.calendarAppearance.isWeekMode)
// {
// break;
// }
}
}