iPhone 视图切换程序(含切换动画)

新建Window-based project,新建文件:

File–New–UIViewController subclass–SwitchViewController、BlueViewController、YellowViewController

File–New–XIB–BlueView、YellowView

 

1. 连接Delegate和SwitchViewController

//  View_SwitcherAppDelegate.h
//
//  Created by Chilly Zhong on 2/6/09.
//

#import <UIKit/UIKit.h>
@class SwitchViewController;

@interface View_SwitcherAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet SwitchViewController *switchViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController;

@end

 

//  View_SwitcherAppDelegate.m
//
//  Created by Chilly Zhong on 2/6/09.
//

#import "View_SwitcherAppDelegate.h"
#import "SwitchViewController.h"

@implementation View_SwitcherAppDelegate

@synthesize window;
@synthesize switchViewController;

– (void)applicationDidFinishLaunching:(UIApplication *)application {   

    // Override point for customization after application launch
    [window addSubview:switchViewController.view];
    [window makeKeyAndVisible];
}

– (void)dealloc {
    [window release];
    [switchViewController release];
    [super dealloc];
}

@end

2. 实现SwitchViewController,用来控制BlueViewController和YellowViewController的显示。

//
//  SwitchViewController.h
//
//  Created by Chilly Zhong on 2/6/09.
//

#import <UIKit/UIKit.h>
@class BlueViewController;
@class YellowViewController;

@interface SwitchViewController : UIViewController {
    IBOutlet BlueViewController *blueViewController;
    IBOutlet YellowViewController *yellowViewController;
}
– (IBAction)switchViews:(id)sender;
@property (nonatomic, retain) BlueViewController *blueViewController;
@property (nonatomic, retain) YellowViewController *yellowViewController;
@end

 

//
//  SwitchViewController.m
//
//  Created by Chilly Zhong on 2/6/09.
//

#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"

@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;

(IBAction)switchViews:(id)sender {
    //Lazy load – we load the Yellow nib the first time the button is pressed
    if(self.yellowViewController == nil)
    {
        YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];
        self.yellowViewController = yellowController;
        [yellowController release];
    }
   

    //注释为无动画切换

    /*if(self.blueViewController.view.superview == nil)
    {
        [yellowViewController.view removeFromSuperview];                     //移走yellowView
        [self.view insertSubview:blueViewController.view atIndex:0];      //显示blueView
    }
    else
    {
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0];
    }*/
   

    //实现动画

    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    if(blueViewController.view.superview == nil)
    {
        [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];
        [blueViewController viewWillAppear:YES];
        [yellowViewController viewWillDisappear:YES];
        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview:self.blueViewController.view atIndex:0];
        [yellowViewController viewDidDisappear:YES];
        [blueViewController viewDidAppear:YES];
    }
    else
    {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                               forView:self.view cache:YES];
        [yellowViewController viewWillAppear:YES];
        [blueViewController viewWillDisappear:YES];
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:self.yellowViewController.view atIndex:0];
        [blueViewController viewDidDisappear:YES];
        [yellowViewController viewDidAppear:YES];
    }
    [UIView commitAnimations];
}

// The designated initializer. Override to perform setup that is required before the view is loaded.
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
– (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad {
    BlueViewController *blueController = [[BlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
    self.blueViewController = blueController;
    [self.view insertSubview:blueViewController.view atIndex:0];    //初始化,显示BlueView
    [blueController release];
}

// Override to allow orientations other than the default portrait orientation.
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

– (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn’t have a superview
    // Release anything that’s not essential, such as cached data
}

– (void)dealloc {
    [yellowViewController release];
    [blueViewController release];   
    [super dealloc];
}

@end

3. 双击BlueView.xib和YellowView.xib设计界面(界面设计随意)。

 

视图切换前后如下图:

blueView yellowView

翻页(Flip)动画速度太快无法捕捉,因此没有截图,从书中截屏了如下动画:

image

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s