【发布时间】:2014-05-31 08:16:35
【问题描述】:
我有一个 UIScrollView,其中有一个 calendarView。我现在想要的是下两个月和前两个月已经加载到滚动视图中。
我有以下两种方法。
-(void)addCalendarToEnd{
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[self sameDateByAddingMonths:1 andDate:scrollDate]];
float xValue = phoneCal.frame.size.width * numberOfCalendarsAfterToday;
phoneCal.frame = CGRectMake(xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
[self.calendarScroll addSubview:phoneCal];
int numberScroll = numberOfCalendarsBeforeToday + numberOfCalendarsAfterToday + 1;
calendarScroll.contentSize = CGSizeMake(numberScroll*phoneCal.bounds.size.width,calendarScroll.bounds.size.height);
[calendarScroll addSubview:phoneCal];
NSLog(@"CONTENT SIZE IS %f",calendarScroll.contentSize.width);
numberOfCalendarsAfterToday++;
}
-(void)addCalenderToBeginning{
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[self sameDateByAddingMonths:-1 andDate:scrollDate]];
float xValue = phoneCal.frame.size.width * numberOfCalendarsBeforeToday;
NSLog(@"XVALUE IS %f",xValue);
phoneCal.frame = CGRectMake(-xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
[self.calendarScroll addSubview:phoneCal];
int numberScroll = numberOfCalendarsBeforeToday + numberOfCalendarsAfterToday + 1;
calendarScroll.contentSize = CGSizeMake(numberScroll*phoneCal.bounds.size.width,calendarScroll.bounds.size.height);
NSLog(@"CONTENT SIZE IS %f",calendarScroll.contentSize.width);
numberOfCalendarsBeforeToday++;
}
addCalendarToEnd 工作正常。但addCalendarToBeginning 不是。
它在正确的位置添加视图。但我不知道我应该如何设置contentSize 的UIScrollView。
谁能帮帮我?
编辑
这就是我在 ViewDidLoad 中所做的事情
scrollDate = [NSDate new];
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.frame = CGRectMake(0, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[NSDate new]];
NSLog(@"PHONE CALL HEIGHT IS %f",phoneCal.frame.size.height);
float xValue = phoneCal.frame.size.width * numberOfCalendarsAfterToday;
phoneCal.frame = CGRectMake(xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
calendarScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, phoneCal.frame.size.width, phoneCal.frame.size.height)];
calendarScroll.delegate = self;
[calendarScroll addSubview:phoneCal];
calendarScroll.backgroundColor = [UIColor clearColor];
calendarScroll.contentSize =CGSizeMake(numberOfCalendarsAfterToday*phoneCal.frame.size.width,phoneCal.frame.size.height);
calendarScroll.contentOffset = CGPointZero;
[self.calendarView addSubview:calendarScroll];
NSLog(@"PHONE CALL HEIGHT NOW IS %f",phoneCal.frame.size.height);
numberOfCalendarsBeforeToday++;
numberOfCalendarsAfterToday++;
【问题讨论】:
-
为什么要在
addCalendarToEnd中添加两次日历? -
@mattsven 但你能帮我弄清楚吗?我正在寻找几天的解决方案!
-
这是你在运行时应该做的事情的顺序。您应该制作 previousMonthView。然后像往常一样将此大小添加到滚动视图的内容大小。然后将滚动视图顶部的所有视图进一步向下移动。然后将 previousMonthView 放在滚动视图的原点(0,0)上。现在您将滚动视图的 contentOffSet 更改为 previousMonthsView 的大小,以便用户觉得您根本没有移动,除了现在他可以向上滚动到前几个月。
-
@StefGeelen,您需要分页吗?换句话说,需要滚动视图捕捉到完整的页面吗?
标签: ios objective-c uiscrollview uiscrollviewdelegate