iOS开发之自定义多控制器的切换
2016-04-19 22:51
555 查看
前言:项目开发中往往会遇到多控制器切换的需求,如下开发了一个demo
1.效果
2.主要代码
// // ViewController.m // moreControllers // // Created by admin on 16/4/19. // Copyright © 2016年 tinghou. All rights reserved. // #define HTColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define HTColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a] #define Screen_width [[UIScreen mainScreen] bounds].size.width #import "ViewController.h" #import "SwitchMenuView.h" #import "SwitchMenuViewModel.h" #import "UIView+Extension.h" #import "leftViewController.h" #import "centerViewController.h" #import "rightViewController.h" @interface ViewController ()<SwitchMenuViewDelegate> @property (strong,nonatomic)SwitchMenuView *segmentMenu; /** 正在显示的控制器 */ @property (nonatomic, weak) UIViewController *showingVc; /** 用来存放子控制器的view */ @property (nonatomic, weak) UIView *contentView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addsegMenu]; } -(void)initChileViewControllers{ UIView *contentView = [[UIView alloc] init]; contentView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64); [self.view addSubview:contentView]; self.contentView = contentView; [self addChildViewController:[[leftViewController alloc] init]]; // [self.childViewControllers[0] didMoveToParentViewController:self]; [self addChildViewController:[[centerViewController alloc] init]]; [self addChildViewController:[[rightViewController alloc] init]]; self.showingVc = self.childViewControllers[0]; [self.contentView addSubview:self.showingVc.view]; } -(void)addsegMenu { SwitchMenuViewModel * titles = [SwitchMenuViewModel sharedsegMentTitles]; titles.titleArray = [NSMutableArray arrayWithObjects:@"左",@"中",@"右",nil]; titles.bgColorArray = [NSMutableArray arrayWithObjects:HTColorRGBA(232, 232, 232,0.3),HTColorRGBA(28, 127, 242, 0.5),nil]; //标题颜色 UIColor * btnTitleColour = [UIColor whiteColor]; //标题字体 // UIFont * font = [UIFont systemFontOfSize:17]; UIFont * font = [UIFont boldSystemFontOfSize:16]; titles.titleEdgeInsets = [NSMutableArray arrayWithObjects:@0,@10,@0,@0, nil]; titles.contentEdgeInsets =[NSMutableArray arrayWithObjects:@0,@30,@0,@0,nil]; titles.titleElements = [NSMutableArray arrayWithObjects:btnTitleColour,font,nil]; _segmentMenu = [[SwitchMenuView alloc]init]; _segmentMenu.layer.borderWidth = 1; _segmentMenu.layer.cornerRadius = 15; _segmentMenu.layer.borderColor = [UIColor whiteColor].CGColor; _segmentMenu.layer.masksToBounds = YES; _segmentMenu.delegate = self; _segmentMenu.height = 30; _segmentMenu.x = 10.0; _segmentMenu.y = 25.0; _segmentMenu.width = Screen_width-20.0; [self.view addSubview:_segmentMenu]; } #pragma mark ---SwitchMenuViewDelegate--- - (void)SwitchMenuView:(SwitchMenuView *)menu didSelectedButtonFromIndex:(int)fromIndex toIndex:(int)toIndex { [self initChileViewControllers]; // 移除其他控制器的view [self.showingVc.view removeFromSuperview]; // 获得控制器的位置(索引) NSUInteger index = toIndex; // 当前控制器的索引 NSUInteger oldIndex = [self.childViewControllers indexOfObject:self.showingVc]; // 添加控制器的view self.showingVc = self.childViewControllers[index]; self.showingVc.view.frame = self.contentView.bounds; [self.contentView addSubview:self.showingVc.view]; // 动画 CATransition *animation = [CATransition animation]; animation.type = @"cube"; animation.subtype = index > oldIndex ? kCATransitionFromRight:kCATransitionFromLeft; animation.duration = 0.5; //非动画 // [self.contentView.layer addAnimation:animation forKey:nil]; //动画 [self.view.layer addAnimation:animation forKey:nil]; } @end
3.项目目录结构
3.demo下载地址
https://github.com/tinghou/my-code/tree/master/moreControllers相关文章推荐
- iOS 基础动画的使用
- ios 音视频处理优化总结
- iOS6的Mail里下拉刷新动画效果是什么原理
- iOS 下拉刷新 MJRefresh 详解
- IOS 图片模糊处理 ------ 直接代码 复制出去就可用 值得标记
- iOS 手势
- iOS开发-你真的会用SDWebImage?
- iOS category内部实现原理
- iOS-工作经验+资料分享(长期更新)
- iOS 各种坐标系对比
- iOS之保存图片到系统相册和从系统相册选取一张或者多张照片
- iOS KVC
- iOS学习笔记-----常用知识点总结
- Xcode 6 新建工程运行在 iOS 7 上下有黑边的问题
- IOS学习之——TableView基础
- IOS学习之——欢迎界面
- iOS开发- 相机(摄像头)获取到的图片自动旋转90度解决办法
- iOS 学习笔记之懒加载
- 动画
- iOS单元测试