Change UIImageView image half way of a flip animation

前端 未结 3 1755
天涯浪人
天涯浪人 2021-02-10 07:48

How can I change the image of a UIImageView on half way of a Flip animation. My code does seem to flip the UIImageView and successfully change the image but it is changing in an

相关标签:
3条回答
  • 2021-02-10 08:36

    For Swift

    UIView.transition(with: imageView,
                      duration: 0.4,
                      options: UIView.AnimationOptions.transitionFlipFromLeft,
                      animations: {
                          imageView.image = newImage
            }, completion: nil)
    
    0 讨论(0)
  • 2021-02-10 08:43

    Try a two steps animation, if you don't like this you can lookup the documentation for concatenating options.

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn
                     animations:^(void) {
                         //do your half rotation here
                         imageView.transform = CGAffineTransformMakeScale(0, 1);
                     }
                     completion:^(BOOL finished) {
                         if(finished){
                             imageView.image = [UIImage imageNamed:@"fb2.jpg"];
                             [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                                              animations:^(void) {
                                                  //do your second half rotation here
                                                  imageView.transform = CGAffineTransformMakeScale(-1, 1);
                                              } completion:^(BOOL finished) {
    
                                              }];
                         }
                     }];
    
    0 讨论(0)
  • 2021-02-10 08:45

    You can use UIView class method + (void)transitionWithView:duration:options:animations:completion: for this animation easily.

    Use code like this for your animation:

    [UIView transitionWithView:imageView
                      duration:0.4
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        //  Set the new image
                        //  Since its done in animation block, the change will be animated
                        imageView.image = newImage;
                    } completion:^(BOOL finished) {
                        //  Do whatever when the animation is finished
                    }];
    

    You can set direction of flip by replacing UIViewAnimationOptionTransitionFlipFromRight with any of the following options:

    UIViewAnimationOptionTransitionFlipFromLeft
    UIViewAnimationOptionTransitionFlipFromRight
    UIViewAnimationOptionTransitionFlipFromTop
    UIViewAnimationOptionTransitionFlipFromBottom
    
    0 讨论(0)
提交回复
热议问题