Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CATransform3D to create flip animation

I'm trying to recreate UIViewAnimationTransitionFlipFromRight (and left). My reason for doing so, as shown below, is to make changes to AVCaptureVideoPreviewLayer in the middle of the animation, when the layer is obstructed. UIViewAnimationTransitionFlipFromRight won't let me stop the animation half way, make session changes, and continue, so here is my best shot at it.

While this works, it's just not the same as UIViewAnimationTransitionFlipFromRight. The layer starts to rotate, but more of a slide, backwards and diagonally (very hard to describe), and then reverses for the second part of the animation. I'm looking for the right side of the layer to flip to the back, and then continue to the left. Instead, the right side starts on the right, rotates to the back, and then rotates to the right again.

What am I doing wrong?

UPDATE: It rotates properly the first time. After that, the problem mentioned above persists. Is there something to do with the AVCaptureVideoPreviewLayer that has to be reset? Not sure, just a guess.

[UIView animateWithDuration:1.5 delay:0.0 
                                options:UIViewAnimationCurveEaseIn 
                             animations:^{
                                 CATransform3D frontTransform = CATransform3DIdentity;
                                 frontTransform.m34 = 1.0 / -850.0;
                                     frontTransform = CATransform3DMakeRotation(M_PI_2,0.0,1.0,0.0); //flip halfway
                                     frontTransform = CATransform3DScale(frontTransform, 0.835, 0.835, 0.835);
                                 previewLayer.transform = frontTransform;

                             }
                             completion:^(BOOL finished){
                                 if (finished) {

                                     [previewLayer setAutomaticallyAdjustsMirroring:NO];
                                     [previewLayer setMirrored:NO];

                                     [session beginConfiguration];
                                     [[self captureManager] setMirroringMode:AVCamMirroringOff];
                                     [session commitConfiguration];

                                     [UIView animateWithDuration:1.5
                                                           delay:0.0 
                                                         options:UIViewAnimationCurveEaseOut 
                                                      animations:^{
                                                          CATransform3D backTransform = CATransform3DIdentity;
                                                          backTransform.m34 = 0.0f;
                                                              backTransform = CATransform3DMakeRotation(M_PI,0.0,1.0,0.0); //finish the flip
                                                              backTransform = CATransform3DScale(backTransform, 1.0, 1.0, 1.0);
                                                          previewLayer.transform = backTransform;
                                                      }
                                                      completion:^(BOOL finished){
                                                              //nothing upon completion
                                                      }
                                      ];
                                 }
                             }
             ];
like image 458
W Dyson Avatar asked Dec 09 '25 23:12

W Dyson


1 Answers

You don't say what you mean by "it's just not the same as UIViewAnimationTransitionFlipFromRight". Are you seeing perspective? I've found that I need to specify the .m34 field first prior to calling the CATransform3D functions in order to get perspective. Set that right after you declare your transform and before your call to CATransform3DMakeRotation.

like image 98
Matt Long Avatar answered Dec 11 '25 13:12

Matt Long



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!