How can I flip a UIImageView?

后端 未结 6 1039
小蘑菇
小蘑菇 2021-02-09 01:58

How can I flip an UIImageView?

相关标签:
6条回答
  • 2021-02-09 02:24

    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];
    }
    0 讨论(0)
  • 2021-02-09 02:32

    You should be able to flip the view vertically with:

    imageView.transform = CGAffineTransformMake(
        1, 0, 0, -1, 0, imageView.bounds.size.height
    );
    
    0 讨论(0)
  • 2021-02-09 02:35

    for horizontally flip

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

    for vertically flip

    imageView.transform = CGAffineTransformScale(imageView.transform, 1.0, -1.0);
    
    0 讨论(0)
  • 2021-02-09 02:46

    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; }  
    
    0 讨论(0)
  • 2021-02-09 02:47
    #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;
    }  
    
    0 讨论(0)
  • 2021-02-09 02:47

    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)    { }];
                         }];
    }
    
    0 讨论(0)
提交回复
热议问题