在iOS开发中经常会创建N个可滑动的VC。
创建一个Super父类,并继承SPPageMenuDelegate ,UIScrollViewDelegate
@interface yourFatherViewController ()<SPPageMenuDelgeate,UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView; //懒加载一个可滑动的ScrollView
@property (nonatomic,strong) SPPageMenu *pageMenu;
@property (nonatomic,strong) MutableArray *myChildrensViewController
@end
- (void)setupPageMenu {
SPPageMenu 初始化你的SPPage类型这里不做多解释
重要的是接下来的这段代码
[pageMenu setItem:@[@"分类vc1",@"分类vc2"];
pageMenu.delegate = self;
pageMenu.bridgeScrollView = self.scrollView //所有分类页的可滑动基础都依靠这一句
self.scrollView.contentOffSet = CGPointMake(宽度*self.pageMenu.selectedIndex,0)
//可滑动页面的数量取决于setItem数组有多少个元素
self.scrollView.contentSize = CGRectMaek(self.dataArr.count*宽度)
//同理
_pageMenu = pageMenu;
[self.view addSubView:pageMenu];
[self.view addSubView:self.scrollView];
UIViewController *vc1 = [[UIViewController alloc]init];
UIViewController *vc2 = [[UIViewController alloc]init];
if self.pageMenu.selectedIndex < self.myChildrensViewController.count {
//选择一个MainVC作为SPPageMenu的主页
UIViewController *basicView = [[UIViewController alloc]init];
[self.scrollView addSubView:basicView];
basicView.frame = CGRectMake(kScreenWidth * self.menuPage.selectedIndex, 0 ,kScreenWidth,kScreenHeight)
self.scrollView.contentOffset = CGPointMake(ScreenWidth*self.pageMenu.selectedItemIndex, 0);
self.scrollView.contentSize = CGSizeMake(self.dataArr.count*ScreenWidth, 0);
//初始化basicView的Frame
//设置self.contentView的内容页宽度
//设置self.contentView的内容偏移大小
}
}
SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
NSLog(@"%zd------->%zd",fromIndex,toIndex){
_index = toIndex;
if (self.scrollView.isDragging){ //判断是否在拖动
if (labs(toIndex - fromIndex > = 2)) //如果fromIndex 于 toIndex 的差大于二
//说明跨界移动VC,此时不显示动画
[self.scrollView setContentOffSet:CGPointMake];
}
//数组越界判断
if (你的子类数量.count <= toIndex){return} //直接return
yourVC = 你的子类[index];
[[yourVC alloc]init];
[_scrollView addSubView:yourVC.view]; //自此父级页面完成
}
子级页面根据产品需求来决定,可以是一个可滑动页面UITableView UICollectionView
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。