#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view1=[[UIView alloc]initWithFrame:CGRectMake(40, 40, 200, 100)];
self.view1.backgroundColor=[UIColor redColor];
self.view2=[[UIView alloc]initWithFrame:self.view1.frame];
self.view2.backgroundColor=[UIColor blueColor];
self.view3=[[UIView alloc]initWithFrame:self.view1.frame];
self.view3.backgroundColor=[UIColor greenColor];
UIView *containview=[[UIView alloc]initWithFrame:self.view1.frame];
[containview addSubview:self.view3];
[containview addSubview:self.view2];
[containview addSubview:self.view1];
[self.view addSubview:containview];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)transform:(UIButton *)sender { [UIView transitionFromView:self.view1 toView:self.view3 duration:2 options:UIViewAnimationOptionShowHideTransitionViews|UIViewAnimationOptionTransitionFlipFromLeft completion:nil];}
我想从第一个视图转换到第三个视图。但是运行却是第一个转换到第二个视图。能不能告诉我为什么。应该怎么修正代码。谢谢。
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view1=[[UIView alloc]initWithFrame:CGRectMake(40, 40, 200, 100)];
self.view1.backgroundColor=[UIColor redColor];
self.view2=[[UIView alloc]initWithFrame:self.view1.frame];
self.view2.backgroundColor=[UIColor blueColor];
self.view3=[[UIView alloc]initWithFrame:self.view1.frame];
self.view3.backgroundColor=[UIColor greenColor];
UIView *containview=[[UIView alloc]initWithFrame:self.view1.frame];
[containview addSubview:self.view3];
[containview addSubview:self.view2];
[containview addSubview:self.view1];
[self.view addSubview:containview];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)transform:(UIButton *)sender { [UIView transitionFromView:self.view1 toView:self.view3 duration:2 options:UIViewAnimationOptionShowHideTransitionViews|UIViewAnimationOptionTransitionFlipFromLeft completion:nil];}
我想从第一个视图转换到第三个视图。但是运行却是第一个转换到第二个视图。能不能告诉我为什么。应该怎么修正代码。谢谢。
1. view2不要提前添加到视图层级上,只有当需要显示时才显示。或者你自己调整containview的子视图的视图层级
2. 把UIViewAnimationOptionShowHideTransitionViews替换为UIViewAnimationOptionLayoutSubviews,让系统去调整containview的子视图的视图层级