How can I flip a UIImageView?

后端 未结 6 1032
小蘑菇
小蘑菇 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];
    }

提交回复
热议问题