Apply reverse CATransform3D to CALayer sublayers that have already been converted

I am using the CATransform3D transform to apply a pseudo 3D perspective effect for presentation.

I use the gyroscope in iPhone 4 to control the conversion options.

In the view I am converting, there are some subspecies:

View with subviews

The result looks something like this:

3D transform in action

My next task is to either prohibit the conversion of the subview to the main transformation, or apply the inverse transformation so that the sub-items remain unaffected.

My goal is to make each sub-point perpendicular to supervision, giving the impression that they are "standing."

The code I use to convert:

- (void)update:(NSTimer *)timer { roll = [[[_manager deviceMotion] attitude] roll]; pitch = [[[_manager deviceMotion] attitude] pitch]; yaw = [[[_manager deviceMotion] attitude] yaw]; CALayer *layer = [self.view layer]; CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, -90 * M_PI / 180.0f, 0.0f, 0.0f, 1.0f); // make landscape again rotationAndPerspectiveTransform.m34 = 1.0 / -500; // apply the perspective rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, roll, 1.0f, 0.0f, 0.0f); rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, yaw, 0.0f, 0.0f, 1.0f); [layer setTransform:rotationAndPerspectiveTransform]; } 

I tried using the following code in an attempt to invert the transformation by applying a 90 degree perspective rotation to the subzone around the Y axis:

 for (UIView *subview in [self.view subviews]) { CALayer *sublayer = [subview layer]; //CATransform3D inverseTransformation = [sublayer transform]; CATransform3D inverseTransformation = CATransform3DIdentity; rotationAndPerspectiveTransform.m34 = 1.0 / -500; // apply perspective inverseTransformation = CATransform3DRotate(inverseTransformation, 90 * M_PI / 180.0f, 0.0f, 1.0f, 0.0f); [sublayer setTransform:inverseTransformation]; } 

but all this can be done, since the subview disappears. If I try to use an existing transform from a layer using [sublayer transform] and apply a 3D rotation to it, then the subview will flicker but not rotate.

My knowledge of Matrix math is small, so I don’t know if this fits correctly.

How can I make subviews sit perpendicular to a super view?

+6
source share
1 answer

I recommend not trying to make a basic approach for calculating each individual conversion job. In addition, it would be simpler using CALayers for each of the small sub-options instead of higher-level UIView objects. The basic approach to creating a 3D model and transforming the whole model should work much better for you.

Start with the main layer, add a sublevel for each of the small squares, convert these sublayers to the desired position, then your update: method specified in your question should work to convert everything without changes. John Blackburn has a nice little tutorial that should help a lot.

+1
source

Source: https://habr.com/ru/post/901136/


All Articles