Image Change Using Button Pressed in iPhone

In this example we will see how to ImageChange using button pressed. So let see how it will work in our application. My previous post you can find out from here KeyBoard example

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “ImageChange”.

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: Open the ImageChangeViewController.h file and make the following changes in the file:


#import <uikit/UIKit.h>

@interface ImageChangeViewController : UIViewController {

UIView *View3;
UIImageView *View1;
UIImageView *View2;


@property (nonatomic, retain) UIView *View3;
@property (nonatomic, retain) UIImageView *View1;
@property (nonatomic, retain) UIImageView *View2;

- (IBAction)MoveImage:(id)sender;




Step 4: Double click the ImageChangeViewController.xib file and open it to the Interface Builder. First drag the UIButton from library and place it to the view window. Select the Button and bring up Connection Inspector and drag Touch Up Inside to the File’s Owner icon and select MoveImage: action. Now save the .xib file and go back to the Xcode.

Step 5: In the ImageChangeViewController.m file make the following changes:


#import "ImageChangeViewController.h"

#define kHeight 320.0
#define kWidth 400.0
#define kTransitionDuration 0.75
#define kTopPlacement 80.0

@implementation ImageChangeViewController

@synthesize View3, View1, View2;

- (void)dealloc
[View1 release];
[View2 release];
[View3 release];
[super dealloc];

- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];


#pragma mark - View lifecycle

- (void)viewDidLoad
[super viewDidLoad];

self.title = NSLocalizedString(@"TransitionsTitle", @"");

CGRect frame = CGRectMake(round((self.view.bounds.size.width - kWidth) / 2.0),
kTopPlacement, kWidth, kHeight);
self.View3 = [[[UIView alloc] initWithFrame:frame] autorelease];
[self.view addSubview:self.View3];

frame = CGRectMake(0.0, 0.0, kWidth, kHeight);
self.View1 = [[[UIImageView alloc] initWithFrame:frame] autorelease];
self.View1.image = [UIImage imageNamed:@"1.png"];
[self.View3 addSubview:self.View1];

CGRect imageFrame = CGRectMake(0.0, 0.0, kWidth, kHeight);
self.View2 = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.View2.image = [UIImage imageNamed:@"2.png"];


- (IBAction)MoveImage:(id)sender
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kTransitionDuration];

[UIView setAnimationTransition:([self.View1 superview] ?
UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)
forView:self.View3 cache:YES];
if ([self.View2 superview])
[self.View2 removeFromSuperview];
[self.View3 addSubview:self.View1];
[self.View1 removeFromSuperview];
[self.View3 addSubview:self.View2];

[UIView commitAnimations];

- (void)viewDidUnload
[super viewDidUnload];

self.View3 = nil;
self.View2 = nil;
self.View1 = nil;


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);




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

You can Download SourceCode from here ImageChange

Leave a Comment:

1 comment
mina says January 21, 2013

i reads Tutorials .you are explorer in program .The goods step in make programs.

Add Your Reply