iphone Flip Image

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:

UIView *containerView;
    UIImageView *mainView;
    UIImageView *flipToView;
@property (nonatomic, retain) UIView *containerView;
@property (nonatomic, retain) UIImageView *mainView;
@property (nonatomic, retain) UIImageView *flipToView;
- (IBAction)flipAction:(id)se
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;
- (void)dealloc
    [mainView release];
    [flipToView release];
    [containerView release];
    [super dealloc];
- (void)viewDidLoad
    [super viewDidLoad];
    CGRect frame = CGRectMake(round((self.view.bounds.size.width – kImageWidth) / 2.0), kTopPlacement, kImageWidth, kImageHeight);
    self.containerView = [[[UIView alloc] initWithFrame:frame] autorelease];
    [self.view addSubview:self.containerView];
    frame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
    self.mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
    self.mainView.image = [UIImage imageNamed:@”Screen.jpg”];
    [self.containerView addSubview:self.mainView];
    CGRect imageFrame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
    self.flipToView = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
    self.flipToView.image = [UIImage imageNamed:@”Screen1.jpg”];
- (void)viewDidUnload
    [super viewDidUnload];
    self.containerView = nil;
    self.flipToView = nil;
    self.mainView = nil;
- (IBAction)flipAction:(id)sender
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:kTransitionDuration];
    [UIView setAnimationTransition:([self.mainView superview] UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
     forView:self.containerView cache:YES];
    if ([flipToView superview])
        [self.flipToView removeFromSuperview];
        [self.containerView addSubview:mainView];
        [self.mainView removeFromSuperview];
        [self.containerView addSubview:flipToView];
    [UIView commitAnimations];
Finally press Build And Go Button

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>