In this tutorial will learn how to flip the image, so to do this will use two Image View and a button i.e. bar button for the flip action, whenever we press flip button image should change. For that had to write the action and then method for that action. This application will be Based on View Based Application and the coding will be done only in ViewController File.
Final Image will look like this:
My project name is FlipImage and is based on View Application, here am creating two variable for Image View so that the image which am using can be placed and also to flip we need two image. am also using View and creating variable for that so that the image View can be placed, after doing this we need bar button for the flip action so am writing the action in .h file and method will be written in .m file, property for all the variables is declared.
Add this to .h file of ViewController:
@property (nonatomic, retain) UIView *containerView;
@property (nonatomic, retain) UIImageView *mainView;
@property (nonatomic, retain) UIImageView *flipToView;
In .m file will set the fixed variable or define the fixed variable just below the import line and above the implementation method and then synthesizing the variable which were created in .h file so that the setter and getter function will be created automatically and then releasing the variable which created. then loading the view, Image View is being created by code and in view load method setting the frame for the image or by creating the container view which will use for transition animation and is created horizontally and then is added to the view. After that the image view is being created and initialize, setting frame and then inserting the image on the image view by giving the name of the file, similarly for the other image view.
After doing all this writing the method for the action, which is declared in .h file in that action am taking if condition to flip the image if [fliptoview superview] then remove image otherwise addsubview.
Add this to ViewController.m file:
#define kImageHeight 200.0
#define kImageWidth 250.0
#define kTransitionDuration 0.75
#define kTopPlacement 80.0
@synthesize containerView, mainView, flipToView;
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:@"Screen.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] UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
if ([flipToView superview])
Finally press Build And Go Button
Recommend the tutorial