Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I flip a UIImageView?

Tags:

iphone

How can I flip an UIImageView?

like image 477
deeplovepan Avatar asked Jul 28 '09 15:07

deeplovepan


People also ask

How do I flip an image in Swift 4?

For anyone wondering, if you want to flip the image vertically just change context. scaleBy(x: -1.0, y: 1.0) to context. scaleBy(x: 1.0, y: -1.0) .

How do I rotate an image in Swift?

Basic Swift Code for iOS AppsStep 1 − Open Xcode→SingleViewApplication→name it RotateImage. Step 2 − Open Main. storyboard, add UIImageView and add 2 buttons as shown below name them ROTATE BY 90 DEGREES AND ROTATE BY 45 DEGREES. Add some sample images to UIImage View.


2 Answers

create two UIImageView frontImageView & backImageView

create one UIView containerView to contain UIImageView

Show frontImageView in the beginning.

after flip, show backImageView

code:

// before flip
frontImageView = [[UIImageView alloc] initWithImage:[UIImage 
                    imageNamed:@"test.png"]];

containerView = [[UIView alloc] initWithFrame:frontImageView.bounds];
containerView.center = CGPointMake(200,200);

[self.view addSubview:containerView];
[containerView addSubview:frontImageView];

-(IBAction)flipButtonClicked:(id)sender
{
     backImageView = [[UIImageView alloc] initWithImage:[UIImage 
                            imageNamed:@"cardback.png"]];
     backImageView.center = frontImageView.center;
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:1.0];
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft 
                            forView:containerView 
                              cache:YES];
     [frontImageView removeFromSuperview];
     [containerView addSubview:backImageView];
     [UIView commitAnimations];
}
like image 59
deeplovepan Avatar answered Oct 15 '22 15:10

deeplovepan


You should be able to flip the view vertically with:

imageView.transform = CGAffineTransformMake(
    1, 0, 0, -1, 0, imageView.bounds.size.height
);
like image 7
teabot Avatar answered Oct 15 '22 13:10

teabot