Change View in iPad

In this application we will see how to change view in iPad.

Step 1: Create a Window base application using template. Give the application name “Windowbase_iPad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand classes and notice Interface Builder created the Windowbase_iPadAppDelegate class for you. Expand Resources and notice the template generated a separate nib, MainWindow.xib, for the “Windowbase_iPad”.

Step 4: We need to add two images in the resource folder. Give the name of the resources “1-1.png”,”2-1.png”.

Step 5: We have added QuartzCore.framework, select Frameworks -> add -> Existing frameworks -> selectQuartzCore.framework.

Step 6: In the Windowbase_iPadAppDelegate.h file, we have added instances of UIView and UIImageView class, and create one IBAction method. So make the following changes in the file.

#import <UIKit/UIKit.h>

@interface Windowbase_iPadAppDelegate : NSObject <UIApplicationDelegate> {
   
        UIWindow *window;
        UIView  *subView;
        UIImageView *view1;
        UIImageView *view2;
        BOOL change;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIView  *subView;

(IBAction)ChangeView:(id)sender;

Step 7: Double click your MainWindow.xib file open it to the Interface Builder. Open the the window and drag view from the library and place it to the window. Next drag Toolbar from the library and place it buttom of the view window. Now select ViewChangeAppDelegate, and bring up connection inspector. Select from subView to the view, and ChangeView to the BarButtonItem. Now save your .xib file and go back to the Xcode.

Step 8: Open the Windowbase_iPadAppDelegate.m file and make the following changes in the file.

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
       
        UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1-1.png" ofType:nil]];
        view1 = [[UIImageView alloc] initWithImage:image1];
        UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2-1.png" ofType:nil]];
        view2 = [[UIImageView alloc] initWithImage:image2];
        view2.hidden = YES;
        [subView addSubview:view1];
        [subView addSubview:view2];
        change = NO;
       
        [window makeKeyAndVisible];
       
        return YES;
}

(void)performTransition
{
       
        CATransition *transition = [CATransition animation];

        transition.duration = 0.75;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
       
        NSString *types[4] = {kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};
        NSString *subtypes[4] = {kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};
        int rnd = random() % 4;
        transition.type = types[rnd];
        if(rnd < 3)     {
                transition.subtype = subtypes[random() % 4];
        }
       
        change = YES;
        transition.delegate = self;
       
        [subView.layer addAnimation:transition forKey:nil];
       
        view1.hidden = YES;
        view2.hidden = NO;
       
        UIImageView *tmp = view2;
        view2 = view1;
        view1 = tmp;
}

(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
        change = NO;
}

(IBAction)ChangeView:(id)sender
{
        if(!change)
        {
                [self performTransition];
        }
}

Step 9: Now compile and run the application in the Simulator.

You can Download SourceCode from here Windowbase_iPad

Leave a Reply

Your email address will not be published. Required fields are marked *