簡體   English   中英

如何翻轉UIImageView?

[英]How can I flip a UIImageView?

如何翻轉UIImageView?

創建兩個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];
}

您應該能夠垂直翻轉視圖:

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

我編輯了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; }  
#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;
}  

使用@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)    { }];
                     }];
}

用於水平翻轉

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

用於垂直翻轉

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM