前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-如何让SPPageMenu滑动

iOS-如何让SPPageMenu滑动

原创
作者头像
Wilbur-L
修改2021-09-03 10:58:58
6010
修改2021-09-03 10:58:58
举报

一·应用场景

在iOS开发中经常会创建N个可滑动的VC。

二·可滑动的本质<UIScrollViewDelegate>

创建一个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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一·应用场景
  • 二·可滑动的本质<UIScrollViewDelegate>
  • 三·实现子级页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档