In this tutorial will learn how to change the image on button click in curl format, this is done by coding. This application will be based on View Based Application and the coding will be done only in View Controller file, Here am not taking the Image View through the Interface Builder, we are doing it through the code. We use Interface Builder only to add toolbar and the Bar Button to the Toolbar and to keep the button in center i have used the space (Flexible Space) on both the side of button so that the button on Bar will be on center.
Final Image will look like this:
My project name is CurlImage and is Based on View based Application. Here i am taking two Image View to add the Image on Image View and also the view so the the image View can be placed on that and when the button is clicked Image will change in curl format, So to do this we have to write the action for the button and will declare the action for the button in .h file and will write the method in .m file and also write the property for the Image View and the View.
Add this to View Controller.h file:
@property (nonatomic, retain) UIView *containerView;
@property (nonatomic, retain) UIImageView *mainView;
@property (nonatomic, retain) UIImageView *flipToView;
After declaring the variable for the Image View and the View, will define some of the constant variable and then will synthesize the variable in .m file. After doing this will add the view and also the image view into the view Did Load method, there will create view and after creating will add it to the Subview and after adding will create the frame for the Image View and also will set the image on it similarly will do for another Image View(Image on Image View is set by using UIImage imageNamed ).
After adding the view and the Image view to the subview will write the action for the button so that when the button is clicked the image will be changed for that we use if condition and through that the image is changed on bar button click.
Add this to View Controller.m file:
@synthesize containerView, mainView, flipToView;
self.title = NSLocalizedString(@"TransitionsTitle", @"");
CGRect frame = CGRectMake(round((self.view.bounds.size.width - kImageWidth) / 2.0),
kTopPlacement, kImageWidth, kImageHeight);
self.containerView = [[[UIView alloc] initWithFrame:frame] autorelease];
frame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
self.mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
self.mainView.image = [UIImage imageNamed:@"Wonder.jpg"];
CGRect imageFrame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
self.flipToView = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.flipToView.image = [UIImage imageNamed:@"Screen1.jpg"];
self.containerView = nil;
self.flipToView = nil;
self.mainView = nil;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationTransition:([self.mainView superview] ?
UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)
if ([self.flipToView superview])
After creating all the variable and also writing the action for the bar button will have to make the connection with Interface Builder and after making the connection will set the color of the tool bar as shown and then will save the changes in Interface Builder and to make the bar button center will use flexible space on both the side of the bar button. You can find the flexible space in library and drop it on both side of bar button.
Make connection in Interface Builder as shown:
Finally press Build And Go button
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.