[self.contentView addSubview:self.lastVC.view];
[self.contentView addSubview:self.currentVC.view];
[self.contentView addSubview:self.willShowVC.view];
self.lastVC.tableView.backgroundColor = [UIColor redColor];
self.currentVC.tableView.backgroundColor = [UIColor cyanColor];
self.willShowVC.tableView.backgroundColor = [UIColor greenColor];
self.lastVC.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - SafeAreaTopHeight);
self.currentVC.view.frame = CGRectMake(0, (ScreenHeight - SafeAreaTopHeight), ScreenWidth, ScreenHeight - SafeAreaTopHeight);
self.willShowVC.view.frame = CGRectMake(0, (ScreenHeight - SafeAreaTopHeight) * 2, ScreenWidth, (ScreenHeight - SafeAreaTopHeight));
- (UIScrollView *)contentView
{
if (!_contentView) {
_contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, ScreenHeight - SafeAreaTopHeight)];
_contentView.contentSize = CGSizeMake(ScreenWidth, (ScreenHeight - SafeAreaTopHeight) * 3);
_contentView.scrollEnabled = NO;
}
return _contentView;
}
- (CUrrentViewController *)lastVC
{
if (!_lastVC) {
_lastVC = [[CUrrentViewController alloc] init];
}
return _lastVC;
}
- (CUrrentViewController *)currentVC
{
if (!_currentVC) {
_currentVC = [[CUrrentViewController alloc] init];
WEAKSELF;
_currentVC.loadMoreBlock = ^{
///中间视图上拉加载
[weakSelf.currentVC openRefresh];
weakSelf.currentIndex ++;
weakSelf.currentIndex = MIN(self.dataArray.count - 1, weakSelf.currentIndex);
[UIView animateWithDuration:0.5 animations:^{
[weakSelf.contentView setContentOffset:CGPointMake(0, (ScreenHeight - SafeAreaTopHeight) * 2)];
} completion:^(BOOL finished) {
[weakSelf reloadAfterTranslation];
}];
};
_currentVC.pull = ^{
///中间视图下啦刷新
[weakSelf.currentVC openRefresh];
weakSelf.currentIndex --;
weakSelf.currentIndex = MAX(0, weakSelf.currentIndex);
[UIView animateWithDuration:0.5 animations:^{
[weakSelf.contentView setContentOffset:CGPointMake(0, 0)];
} completion:^(BOOL finished) {
[weakSelf reloadAfterTranslation];
}];
};
}
return _currentVC;
}
- (CUrrentViewController *)willShowVC
{
if (!_willShowVC) {
_willShowVC = [[CUrrentViewController alloc] init];
}
return _willShowVC;
}
/// 移动完成之后,要恢复原位,并切换展示的数据
- (void)reloadAfterTranslation
{
NSArray *array = self.dataArray[self.currentIndex];
[self.currentVC reloadWithData:array];
if (self.currentIndex - 1 >= 0) {
NSArray *lastArray = self.dataArray[self.currentIndex - 1];
[self.lastVC reloadWithData:lastArray];
}
if (self.currentIndex + 1 < self.dataArray.count) {
NSArray *nextArray = self.dataArray[self.currentIndex + 1];
[self.willShowVC reloadWithData:nextArray];
}
if (self.currentIndex == 0) {
[self.currentVC closeHeaderRefresh];
}
if (self.currentIndex == self.dataArray.count - 1) {
[self.currentVC closeFooterLoad];
}
//将偏移量恢复到原位,展示中间的视图
[self.contentView setContentOffset:CGPointMake(0, (ScreenHeight - SafeAreaTopHeight))];
}
该逻辑可以应用于图片轮播