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:
@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;
@end
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:
#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];
}
else
{
[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);
}
@end
Step 6: Now compile and run the application in the Simulator.
You can Download SourceCode from here ImageChange