Recently, there is a need to swing the UIView around the Y axis. First, we thought of using CATransform3D for rotation. However, the following two problems are encountered during use.
Problem 1: After rotation, the target View can only see half of the UI. For example, if you rotate around the Y axis, you can only see the left half side or the right half side.
Question 2: the code for swinging an animation is as follows:
CATransform3D rotationTransform = CATransform3DIdentity;
RotationTransform = CATransform3DRotate (rotationTransform, 0.25 * M_PI_2, 0.0f, 1.0f, 0.0f );
WeakSelf. viewHightScoreAvatar. layer. transform = rotationTransform;
[UIView animateWithDuration: 2.0 delay: 0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations: ^ {
CATransform3D rotationTransform2 = CATransform3DIdentity;
RotationTransform2 = CATransform3DRotate (rotationTransform2,-0.25 * M_PI_2, 0.0f, 1.0f, 0.0f );
WeakSelf. viewHightScoreAvatar. layer. transform = rotationTransform2;
} Completion: ^ (BOOL finished ){
}];
After multiple searches for information and constant attempts, I finally found the problem. The solution is as follows:
For Question 1: You can set the zPosition of CALayer to solve this problem. zPosition indicates the hierarchy of UIView on the screen during UI rendering. The larger the zPosition, the higher the zPosition.
Problem 2: You can solve this problem by setting m34 of CATransform3D. m34 indicates the perspective effect. The default value is 0 (1/D), indicating infinite distance (D infinity ). The smaller D, that is, the closer m34 is to 1, the more obvious the effect is.
The modified code is as follows:
CATransform3D rotationTransform = CATransform3DIdentity;
RotationTransform. m34 =-1.0f/200366f; // note that this should be called before CATransform3DRotate; otherwise, it will not work.
RotationTransform = CATransform3DRotate (rotationTransform, 0.25 * M_PI_2, 0.0f, 1.0f, 0.0f );
WeakSelf. viewHightScoreAvatar. layer. transform = rotationTransform;
WeakSelf. viewHightScoreAvatar. layer. zPosition = 100;
[UIView animateWithDuration: 2.0 delay: 0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations: ^ {
CATransform3D rotationTransform2 = CATransform3DIdentity;
RotationTransform2.m34 =-1.0f/200366f;
RotationTransform2 = CATransform3DRotate (rotationTransform2,-0.25 * M_PI_2, 0.0f, 1.0f, 0.0f );
WeakSelf. viewHightScoreAvatar. layer. transform = rotationTransform2;
WeakSelf. viewHightScoreAvatar. layer. zPosition = 100;
} Completion: ^ (BOOL finished ){
}];