//当offset大于(x-1.5)高度或小于0.5高度时候 ,这样需要保证size必须大于3倍高度
if(currentOffset.x<.5*self.frame.size.height||contentH- currentOffset.x<1.5*self.frame.size.height){
//这个时候需要重设中心
}
重设中心需要做的事情是,把偏移移动到centent得中心位置,那必然会导致一定数量的改变,假设这个值为X,那为了保证视图看起来是未移动的,那必然,要让子视图(月视图),都移动响应的X高度,因此:重设中心的代码就是:
float cha=self.contentOffset.y-centerOffsetY;
self.contentOffset = CGPointMake(currentOffset.x, centerOffsetY);//偏移移动到中心
for (UIView* view in _visibleMonthView) {
CGPoint center = view.center;
center.y -= cha;
view.center = center;
}
//添加上面的
MonthView *firstMonth=[_visibleMonthView firstObject];
CGFloat upEdge = CGRectGetMinY([firstMonth frame]);
while (upEdge>minX) {
[self placeNewMonthViewOnTop:upEdge];
firstMonth=[_visibleMonthView firstObject];
upEdge = CGRectGetMinY([firstMonth frame]);
}
//添加下面
MonthView *lastMonth=[_visibleMonthView lastObject];
CGFloat bottonEdge=CGRectGetMaxY(lastMonth.frame);
while (bottonEdge
[self placeNewMonthViewOnBottom:bottonEdge];
lastMonth=[_visibleMonthView lastObject];
bottonEdge = CGRectGetMaxY([lastMonth frame]);
}
-(void)placeNewMonthViewOnTop:(float)topEdge
{
MonthView *month=[_visibleMonthView firstObject];
MonthView *newMonth;
if (month==nil) {//没有
newMonth=[[MonthView alloc] initWithDate:[NSDate date]];
}else{
newMonth=[[MonthView alloc] initWithDate:[month previourMonth]];
}
float originY=topEdge-newMonth.frame.size.height;
CGRect r=newMonth.frame;
r.origin.y=originY;
newMonth.frame=r;
[self addSubview:newMonth];
[_visibleMonthView insertObject:newMonth atIndex:0];
}
-(void)placeNewMonthViewOnBottom:(float)bottomEdge
{
MonthView *month=[_visibleMonthView lastObject];
MonthView *newMonth;
if (month==nil) {//没有
newMonth=[[MonthView alloc] initWithDate:[NSDate date]];
}else{
newMonth=[[MonthView alloc] initWithDate:[month nextMonth]];
}
CGRect r=newMonth.frame;
r.origin.y=bottomEdge;
newMonth.frame=r;
[self addSubview:newMonth];
[_visibleMonthView addObject:newMonth];
}
//删除上面多余
firstMonth=[_visibleMonthView firstObject];
while (CGRectGetMaxY(firstMonth.frame)
[firstMonth removeFromSuperview];
[_visibleMonthView removeObject:firstMonth];
firstMonth=[_visibleMonthView firstObject];
}
//删除下面多余
lastMonth=[_visibleMonthView lastObject];
while (CGRectGetMinY(lastMonth.frame)>maxX) {
[lastMonth removeFromSuperview];
[_visibleMonthView removeObject:lastMonth];
lastMonth=[_visibleMonthView lastObject];
} dayView.frame=CGRectMake(tag%7*w+offsetX, offsetY+10+ROWHEIGHT*(tag/7), ROWHEIGHT-20, ROWHEIGHT-20);
这样就完成了日视图的布局,其他附属视图如线条、月标题都可以如此设置。