如何翻转UIImageView?

时间:2009-07-28 15:04:49

标签: iphone

如何翻转UIImageView?

6 个答案:

答案 0 :(得分:17)

创建两个UIImageView frontImageView& backImageView

创建一个UIView containerView以包含UIImageView

在开头显示frontImageView。

翻转后,显示backImageView

代码:

// 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];
}

答案 1 :(得分:7)

您应该可以使用以下方式垂直翻转视图:

imageView.transform = CGAffineTransformMake(
    1, 0, 0, -1, 0, imageView.bounds.size.height
);

答案 2 :(得分:6)

我编辑了Sound Blasters代码以返回UIImageView。但是,此代码不允许您同时垂直和水平翻转图像。对此的修复应该相当容易。

- (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; }  

答案 3 :(得分:4)

#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;
}  

答案 4 :(得分:3)

使用@Fellowsoft代码我创建了一个方法旋转动画。

感谢 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)    { }];
                     }];
}

答案 5 :(得分:2)

用于水平翻转

imageView.transform = CGAffineTransformScale(imageView.transform, -1.0, 1.0);

垂直翻转

imageView.transform = CGAffineTransformScale(imageView.transform, 1.0, -1.0);