自定义控制器的切换-动画
2016-02-09 18:07
387 查看
效果:
![](http://img.blog.csdn.net/20160209180357547?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
// // ViewController.m // 0209-自定义控制器的切换 // // Created by yongkaidong on 16/2/9. // Copyright © 2016年 com.yongkaidong. All rights reserved. // #import "ViewController.h" #import "OneViewController.h" #import "TwoViewController.h" #import "ThreeViewController.h" @interface ViewController () /** 正在显示的控制器 */ @property (nonatomic, weak) UIViewController *showingVc; /** 用来存放子控制器的view */ @property (nonatomic, weak) UIView *contentView; @end @implementation ViewController /** * 懒加载创建用来包裹子控件view的contentView */ - (UIView *)contentView { if (_contentView == nil) { 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; } return _contentView; } - (void)viewDidLoad { [super viewDidLoad]; // 通过addChildViewController添加的控制器都会存在于childViewControllers数组中 [self addChildViewController:[[OneViewController alloc] init]]; [self addChildViewController:[[TwoViewController alloc] init]]; [self addChildViewController:[[ThreeViewController alloc] init]]; } - (IBAction)buttonClick:(UIButton *)sender { // 移除其他控制器的view [self.showingVc.view removeFromSuperview]; // 获得控制器的位置(索引) NSUInteger index = [sender.superview.subviews indexOfObject:sender]; // 当前控制器的索引 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]; } @end
相关文章推荐
- Python运算符优先级
- spring【1】(监听器)
- CentOS 7 下安装LEMP服务(Nginx、MariaDB/MySQL和PHP)
- 操作select的option隐藏或显示的插件
- centos 7.0 编译安装php 7.0.3
- cocos2d-x Auto-batching 浅浅的”深入分析”
- Jquery前端封装之一个块在整个浏览器的拖拽
- checkbox 全选/反选功能的 jquery插件(支持多页保持选择)
- 加载Cocostudio的UI之后,下方按钮无法点击的解决方法
- cocos2d-x3.0 加载Cocostudio的UI后,按钮无法点击的解决方法
- HDU 2528 Area (求直线与线段的交点后求面积)
- 我的博客
- 说说objcRuntime的一些妙用(class_addMethod,class_replaceMethod)
- 自定义控制器切换
- 红螺寺踏春
- java : jsoup 网页 table 解析范例
- win8.1安装veket的方法
- [自学java笔记] day1 chapter4~
- LightOJ 1109 - False Ordering (因子数打表排列)
- supervisor