How can I flip an UIImageView?
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) .
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.
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];
}
You should be able to flip the view vertically with:
imageView.transform = CGAffineTransformMake(
1, 0, 0, -1, 0, imageView.bounds.size.height
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With