t1ViewController.h 的内容如下:(t1ViewController是根控制器, 我这里是创建工程的时候默认的,也可以使用, self.window.rootViewController方法指定根控制器)
#import <UIKit/UIKit.h>
@interface t1ViewController : UIViewController
@property (nonatomic,retain) IBOutlet UIViewController *myview;
@property (nonatomic,retain) IBOutlet UIViewController *myview2;
- (IBAction)clickme:(id)sender;
@end
t1ViewController.m (文件内容如下)
#import "t1ViewController.h"
#import "yellow.h" //uiviewcontroller内容,可以为空,但为了有效果,背景设置不同的色
#import "blue.h" uiviewcontroller内容,可以为空,但为了有效果,背景设置不同的色
@implementation t1ViewController
@synthesize myview;
@synthesize myview2;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
blue *blueController = [[blue alloc] initWithNibName:@"blue" bundle:nil];
self.myview2 = blueController;
[self.view insertSubview:blueController.view atIndex:0];
yellow *yellowController = [[yellow alloc] initWithNibName:@"yellow" bundle:nil];
self.myview = yellowController;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (IBAction)clickme:(id)sender { //这个action在根控制器,拖一个按钮,并关联clickme方法。
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
UIViewController *curr = nil;
UIViewController *going = nil;
if(self.myview.view.superview == nil)
{
curr = myview2;
going = myview;
}
else
{
curr = myview;
going = myview2;
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[myview viewWillAppear:YES];
[myview2 viewWillDisappear:YES];
[myview2.view removeFromSuperview];
[self.view insertSubview:myview.view atIndex:0];
[myview viewDidDisappear:YES];
[myview2 viewDidAppear:YES];
[UIView commitAnimations];
}
@end
有问题Q我加VX:18618146379 http://blog.fendong.cc