How can I flip an UIImageView?
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
);
for horizontally flip
imageView.transform = CGAffineTransformScale(imageView.transform, -1.0, 1.0);
for vertically flip
imageView.transform = CGAffineTransformScale(imageView.transform, 1.0, -1.0);
I've edited Sound Blasters code a bit to return an UIImageView. However, this code does not allow you to flip the image vertically and horizontally at the same time. A fix for this should be rather easy though.
- (UIImageView *) flipImage:(UIImageView *)originalImage Horizontal:(BOOL)flipHorizontal {
if (flipHorizontal) {
originalImage.transform = CGAffineTransformMake(originalImage.transform.a * -1, 0, 0, 1, originalImage.transform.tx, 0);
}else {
originalImage.transform = CGAffineTransformMake(1, 0, 0, originalImage.transform.d * -1, 0, originalImage.transform.ty);
}
return originalImage; }
#import <QuartzCore/QuartzCore.h>
...
- (UIImage *) flipImageVertically:(UIImage *)originalImage direction:(NSString *)axis {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
UIGraphicsBeginImageContext(tempImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(0, 0, 0, 0, 0, 0);
if([axis isEqualToString:@"x"])
{
flipVertical = CGAffineTransformMake(
1, 0, 0, -1, 0, tempImageView.frame.size.height
);
}
else if([axis isEqualToString:@"y"] )
{
flipVertical = CGAffineTransformMake(
-1, 0, 0, 1, tempImageView.frame.size.width, 0
);
}
CGContextConcatCTM(context, flipVertical);
[tempImageView.layer renderInContext:context];
UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[tempImageView release];
return flipedImage;
}
Using @Fellowsoft code I have create a method rotation Animation.
thanks to Fellowsoft!!
-(void)rotateImage:(UIImageView*)image withDuration:(float)duration isHorizzonal:(BOOL)horizontal{
[UIView animateWithDuration:duration/2.0 animations:^{image.transform = horizontal?
CGAffineTransformMake(image.transform.a * -1, 0, 0, 1, image.transform.tx, 0)
:CGAffineTransformMake(1, 0, 0, image.transform.d * -1, 0, image.transform.ty);}
completion:^(BOOL finished)
{
//half animation
[UIView animateWithDuration:duration/2.0 animations:^{image.transform = horizontal?
CGAffineTransformMake(image.transform.a * -1, 0, 0, 1, image.transform.tx, 0)
:CGAffineTransformMake(1, 0, 0, image.transform.d * -1, 0, image.transform.ty);}
completion:^(BOOL finished) { }];
}];
}