所以,我设置了以下场景:
视图控制器.h
#import <UIKit/UIKit.h>
#import "MyBoxViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) MyBoxViewController *activeBox;
@property (strong, nonatomic) MyBoxViewController *box1;
@property (strong, nonatomic) MyBoxViewController *box2;
- (IBAction)SwitchViews:(id)sender;
@end
视图控制器.m
#import "ViewController.h"
@implementation ViewController
@synthesize activeBox;
@synthesize box1;
@synthesize box2;
- (void)viewDidLoad {
[super viewDidLoad];
// Define the to sub views and the container
[self setBox1:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]];
[[[self box1] view] setBackgroundColor:[UIColor blueColor]];
[[[self box1] titleLabel] setText:@"Box Number 1"];
[[[self box1] view] setFrame:CGRectMake(20, 20, 200, 200)];
[self setBox2:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]];
[[[self box2] view] setBackgroundColor:[UIColor orangeColor]];
[[[self box2] titleLabel] setText:@"Box Number 2"];
[[[self box2] view] setFrame:CGRectMake(40, 40, 200, 200)];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
[containerView addSubview:[[self box1] view]];
[self setActiveBox:[self box1]];
[[self view] addSubview:containerView];
}
- (IBAction)SwitchViews:(id)sender {
if ([self activeBox] == [self box1]) {
// switch from 1 to 2
[UIView transitionFromView:[[self box1] view]
toView:[[self box2] view]
duration:1
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:nil];
[self setActiveBox:[self box2]];
} else {
// switch from 2 to 1
[UIView transitionFromView:[[self box2] view]
toView:[[self box1] view]
duration:1
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:nil];
[self setActiveBox:[self box1]];
}
}
@end
MyBoxViewController.h
#import <UIKit/UIKit.h>
@interface MyBoxViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@end
MyBoxViewController.m
#import "MyBoxViewController.h"
@implementation MyBoxViewController
@synthesize titleLabel;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
请注意,在我的 . 上ViewController.xib
,我添加了一个UIButton
绑定到-(IBAction)SwitchViews:(id)sender
.
对我来说唯一突出的是,当我激活 时[UIView transition...]
,我设置的框架会保持不变。我在您提供的代码中没有看到指定框架的任何地方。
除此之外,在我的代码中,我得到一个蓝色矩形 (20,20,200,200),单击按钮将其翻转以显示橙色矩形 (40,40,200,200)。
这对你有帮助吗?