そこで、次のシナリオを設定しました。
ViewController.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
ViewController.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) が表示されます。
それは役に立ちますか?