关于transitionFromViewController的一些用法
2015-09-06 14:55
417 查看
很多时候一个UIViewController有很多的小的子view,这些view很多时候被覆盖在最后,我们一般在最外层addSubview 添加view这些view大多不会一直处于在界面上显示,一般只有在某些时候才会显示,虽然这些view很少出现,但却常常把他们放在内存中,当内存警告时,只能删除他们
这个时候用transitionFromViewController
对于那些暂时不需要显示的subview,通过addchildviewcontroller 把subviewcontroller加进去,同时调用self addchildviewcontroller:需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
相应的view对应相应的controller
当某个子view没有显示的时候,将不会load,减少内存的使用,
当内存紧张的时候,没有被load的view辉被释放,优化了内存,
[self
transitionFromViewController:_currentViewController
toViewController:third
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
third;
}
else {
_currentViewController = oldViewController;
}
}];
参数1,当前的viewcontroller ,参数二,显示的controller 参数三,过度时间,参数4,过度效果, block语句块,
过度完成后设置当前的controller
简单的使用如下:
- (void)viewDidLoad {
[super
viewDidLoad];
first = [[FirstViewController
alloc]init];
[self
addChildViewController:first];
second = [[SecondViewController
alloc]init];
[self
addChildViewController:second];
third =[[ThirdViewController
alloc]init];
[self
addChildViewController:third];
[_AddViews
addSubview:first.view];
_currentViewController =
first;
}
- (IBAction)ThreeBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
third ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:third
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
third;
}
else {
_currentViewController = oldViewController;
}
}];
}
- (IBAction)SecondBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
second ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:second
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
second;
}
else {
_currentViewController = oldViewController;
}
}];
}
- (IBAction)OneBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
first ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:first
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
first;
}
else {
_currentViewController = oldViewController;
}
}];
}
这个时候用transitionFromViewController
对于那些暂时不需要显示的subview,通过addchildviewcontroller 把subviewcontroller加进去,同时调用self addchildviewcontroller:需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
相应的view对应相应的controller
当某个子view没有显示的时候,将不会load,减少内存的使用,
当内存紧张的时候,没有被load的view辉被释放,优化了内存,
[self
transitionFromViewController:_currentViewController
toViewController:third
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
third;
}
else {
_currentViewController = oldViewController;
}
}];
参数1,当前的viewcontroller ,参数二,显示的controller 参数三,过度时间,参数4,过度效果, block语句块,
过度完成后设置当前的controller
简单的使用如下:
- (void)viewDidLoad {
[super
viewDidLoad];
first = [[FirstViewController
alloc]init];
[self
addChildViewController:first];
second = [[SecondViewController
alloc]init];
[self
addChildViewController:second];
third =[[ThirdViewController
alloc]init];
[self
addChildViewController:third];
[_AddViews
addSubview:first.view];
_currentViewController =
first;
}
- (IBAction)ThreeBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
third ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:third
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
third;
}
else {
_currentViewController = oldViewController;
}
}];
}
- (IBAction)SecondBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
second ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:second
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
second;
}
else {
_currentViewController = oldViewController;
}
}];
}
- (IBAction)OneBtn:(id)sender {
NSLog(@"%@",_currentViewController);
if (_currentViewController ==
first ) {
return;
}
UIViewController *oldViewController =
_currentViewController;
[self
transitionFromViewController:_currentViewController
toViewController:first
duration:0.1
options:UIViewAnimationOptionTransitionNone
animations:^{
}
completion:^(BOOL finished) {
if (finished) {
_currentViewController =
first;
}
else {
_currentViewController = oldViewController;
}
}];
}
相关文章推荐
- iOS 动画Animation详解, UIView动画,CALayer动画
- 一个折线图表类
- (转) iOS深入学习(Block全面分析)
- 短路求值新用处
- Java 8 特性 – 终极手册
- 培养好美工的“方法论”
- oracle 存储过程 技巧
- Android实战简易教程-第五十五枪(窃听风云之电话录音上传)
- 马哥-51CTO-Linux培训-0830
- 数据结构之二叉树(遍历、建立、深度)
- hdu1078基础dp
- Makefile中的常用函数
- 苹果appstore审核拒绝理由列举
- MySQL常用的命令大全
- (转)IOS笔记 #pragma mark的用法
- php——<<<start运算符
- c#版 mqtt 3.1.1 client 实现
- 各种主流色彩模式及其概念
- nodeJS删除文件
- noip 2012 国王游戏