Using CATransform3D to create flip animation

纵然是瞬间 提交于 2020-01-13 13:11:05

问题


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
                                                      }
                                      ];
                                 }
                             }
             ];

回答1:


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.




回答2:


I'm not entirely sure, but perhaps you should probably reset the previewLayer.transform to CATransform3DIdentity when you complete the animation? That might be why you are seeing the strange reverse actions the second time you run it.



来源:https://stackoverflow.com/questions/5606467/using-catransform3d-to-create-flip-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!