iOS多控制器实现带滑动动画第1/2页
主控制器,管理控制器.h文件
//宏 #definekScreenWidth[UIScreenmainScreen].bounds.size.width #definekScreenHeight[UIScreenmainScreen].bounds.size.height #import"MYMainViewController.h" #import"MYFirstViewController.h" #import"MYSecondViewController.h" #import"MYThirdViewController.h" @interfaceMYMainViewController()//控制器名 @property(nonatomic,strong)NSArray*VcNames; //选择栏 @property(nonatomic,strong)UIView*clickBar; //底部容器scrollView @property(strong,nonatomic)UIScrollView*containerScrollerView; @end
.m文件
底部scrollView,用于滑动
@implementationMYMainViewController -(UIScrollView*)containerScrollerView { if(!_containerScrollerView){ _containerScrollerView=[[UIScrollViewalloc]init]; _containerScrollerView.pagingEnabled=YES; _containerScrollerView.showsVerticalScrollIndicator=NO; _containerScrollerView.showsHorizontalScrollIndicator=NO; _containerScrollerView.contentSize=CGSizeMake(kScreenWidth*self.VcNames.count,kScreenHeight); _containerScrollerView.backgroundColor=[UIColorwhiteColor]; _containerScrollerView.delegate=self; } return_containerScrollerView; }
初始化顶部选择栏
//三个子控制器 -(NSArray*)VcNames { if(!_VcNames){ _VcNames=@[@"控制器一",@"控制器二",@"控制器三"]; } return_VcNames; } //点击选择栏 -(UIView*)clickBar { if(!_clickBar){ _clickBar=[[UIViewalloc]init]; _clickBar.backgroundColor=[UIColorlightGrayColor]; CGFloatwidth=kScreenWidth/3; CGFloatheight=44; //初始化按钮 for(NSIntegerindex=0;index<3;index++){ UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom]; [buttonsetTitle:self.VcNames[index]forState:UIControlStateNormal]; [buttonsetTitleColor:[UIColorblueColor]forState:UIControlStateNormal]; button.frame=(CGRect){width*index,0,width,height}; [buttonaddTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside]; //绑定tag值 button.tag=index; [_clickBaraddSubview:button]; } } return_clickBar; }
viewDidLoad
-(void)viewDidLoad{ [superviewDidLoad]; self.edgesForExtendedLayout=0; //初始化选择栏 [selfinitClickBar]; //初始化底部scrollView容器 [selfinitScrollViewContainer]; //初始化子控制器 [selfaddChildControllers]; }
添加子控制器,初始化UI
//按钮选择栏 -(void)initClickBar { [self.viewaddSubview:self.clickBar]; self.clickBar.frame=(CGRect){0,0,[UIScreenmainScreen].bounds.size.width,44}; } //初始化滑动容器 -(void)initScrollViewContainer { [self.viewaddSubview:self.containerScrollerView]; self.containerScrollerView.frame=CGRectMake(0,44,kScreenWidth,kScreenHeight); } //添加子控制器 -(void)addChildControllers { //为了方便直观,在此处设置背景色(实际开发中,不能在这里设置,原因是这里只要调用到了控制器的view属性,该控制器将会执行viewDidLoad方法,相当于直接一开始就将三个控制器的所有UI和网络请求全加载完了,负荷会相当重) MYFirstViewController*firstVc=[[MYFirstViewControlleralloc]init]; firstVc.view.backgroundColor=[UIColorredColor]; [selfaddChildViewController:firstVc]; MYSecondViewController*secondVc=[[MYSecondViewControlleralloc]init]; secondVc.view.backgroundColor=[UIColorblueColor]; [selfaddChildViewController:secondVc]; MYThirdViewController*thirdVc=[[MYThirdViewControlleralloc]init]; thirdVc.view.backgroundColor=[UIColoryellowColor]; [selfaddChildViewController:thirdVc]; //默认展示第一个子控制器 [selfscrollViewDidEndDecelerating:self.containerScrollerView]; }
按钮点击事件实现,代理方法实现