多页面嵌套的实现方式
1、底部是一个UIScrollView,然后在UIScrollView上添加视图View;
2、底部是一个UICollectionView,然后在cell上添加各种视图View;
两种方式的区别就是,UICollectionView的重用机制,可以减小内存中读入的数据
3、底部是个UIController,然后添加UIScrollView,在UIController上添加目标UIController,在UIScrollView上添加目标UIController的view;
4、底部是个UIController,然后添加UICollectionView,在UIController上添加目标UIController,在UICollectionView上添加目标UIController的view;
可以保留UIcontroller的生命周期
JXCategoryView的实现
JXCategoryListContainerView继承与UIView
内部UI实现
initializeViews方法 创建UI视图
// 创建底层UIController
_containerVC = [[JXCategoryListContainerViewController alloc] init];
self.containerVC.view.backgroundColor = [UIColor clearColor];
[self addSubview:self.containerVC.view];
// 添加视图
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
if (self.delegate &&
[self.delegate respondsToSelector:@selector(scrollViewClassInlistContainerView:)] &&
[[self.delegate scrollViewClassInlistContainerView:self] isKindOfClass:object_getClass([UICollectionView class])]) {
_collectionView = (UICollectionView *)[[[self.delegate scrollViewClassInlistContainerView:self] alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
}else {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
}
self.collectionView.pagingEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
self.collectionView.scrollsToTop = NO;
self.collectionView.bounces = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
if (@available(iOS 10.0, *)) {
self.collectionView.prefetchingEnabled = NO;
}
if (@available(iOS 11.0, *)) {
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[self.containerVC.view addSubview:self.collectionView];
//让外部统一访问scrollView
_scrollView = _collectionView;
// 第一次读入视图的时候,添加UIController
list = [self.delegate listContainerView:self initListForIndex:index];
if ([list isKindOfClass:[UIViewController class]]) {
[self.containerVC addChildViewController:(UIViewController *)list];
}
// 视图重用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor whiteColor];
for (UIView *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
}
id<JXCategoryListContentViewDelegate> list = _validListDict[@(indexPath.item)];
[list listView].frame = cell.contentView.bounds;
[cell.contentView addSubview:[list listView]];
return cell;
// 触发目标Controller的生命周期
[listVC beginAppearanceTransition:YES animated:NO];
WMPageController
首先子视图要继承WMPageController类
内部创建一个UIScrollview用于加载,我们需要展示的子视图;
在每次需要展示视图时候,通过addChildViewController添加视图;
// 把视图添加到当前Controller上
- (void)wm_addCachedViewController:(UIViewController *)viewController atIndex:(NSInteger)index {
[self addChildViewController:viewController];
viewController.view.frame = [self.childViewFrames[index] CGRectValue];
[viewController didMoveToParentViewController:self];
[self.scrollView addSubview:viewController.view];
[self willEnterController:viewController atIndex:index];
[self.displayVC setObject:viewController forKey:@(index)];
}
在视图消失以后,通过removeFromParentViewController移除控制器;
- (void)wm_removeViewController:(UIViewController *)viewController atIndex:(NSInteger)index {
[self wm_rememberPositionIfNeeded:viewController atIndex:index];
[viewController.view removeFromSuperview];
[viewController willMoveToParentViewController:nil];
[viewController removeFromParentViewController]
[self.displayVC removeObjectForKey:@(index)];
// 放入缓存
if (self.cachePolicy == WMPageControllerCachePolicyDisabled) {
return;
}
if (![self.memCache objectForKey:@(index)]) {
[self willCachedController:viewController atIndex:index];
[self.memCache setObject:viewController forKey:@(index)];
}
}
横向对比
1:WMPageController 为了保持生命周期的完整性,每次都需要对UIController进行操作,增加了性能的消耗;
2:WMPageController底层为UIScrollView,没有进行视图的复用,如果作为首页,会占用较多的内存;
3:使用WMPageController必须要继承WMPageController,对工程的影响面较大,切入性要求较高;
网友评论