What is CATransform3D in Ios.

Source: Internet
Author: User

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 ){


}];


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.