Catransformlayer
When we are constructing complex 3D things, it is very convenient to organize independent elements. For example, you want to create a child's arm: you need to determine which part is the child's wrist, which part is the child's forearm, which part is the child's elbow, which part is the child's upper arm, which part is the child's shoulder and so on.
It is, of course, permissible to move each area independently. Pointing at the elbow moves the forearm and hands, not the shoulders. The Core animation layer makes it easy for you to make a transformation in a 2D environment, but in 3D it is unlikely, because all layers have their children in a single scene (the fifth chapter "transformation" is mentioned).
Catransformlayer solves this problem, Catransformlayer is different from ordinary calayer because it cannot display its own content. Only if there is a transform that has a scoped sublayer does it really exist. Catransformlayer does not plane its sublayers, so it can be used to construct a hierarchical 3D structure, such as my arm example.
Creating an arm with code requires quite a bit of code, so I'll just show it a little bit easier: in the fifth chapter of the cube example, we'll rotate the Camara to solve the layer flatness problem rather than the sublayertransform in the cube sample code. This is a very good trick, but only scoped on a single object, if your scene contains two cubes, then we cannot rotate them alone with this technique.
So, let's try Catransformlayer, the first question is: In the fifth chapter, we use multiple views to construct our cubes, rather than separate layers. We cannot place a boarding map layer in a layer that does not have a host chart without disrupting the existing view hierarchy. We can create a new UIView subclass on top of Catransformlayer (using the +layerclass method). However, to simplify the case, we just rebuilt a separate layer instead of using the view. This means that we cannot display buttons and labels on the cube surface as in the fifth chapter, but we do not have this feature now.
Listing 6.5 is the code. We place the cube in the same basic logic that we used in chapter fifth. But it's not as straightforward as before to add the façade to the host layer of the container view, we put them in a catransformlayer to create a separate cube object, and then put two such cubes into the container. We randomly stain them to distinguish them from each other, so that they are not separated by labels or light. Figure 6.5 shows the result of the operation.
Listing 6.5 assembling a 3D layer system with Catransformlayer
@interfaceViewcontroller () @property (nonatomic, weak) Iboutlet UIView*Containerview;@end @implementationViewcontroller-(Calayer *) Facewithtransform: (catransform3d) transform{//Create cube face layerCalayer *face =[Calayer layer]; Face.frame= CGRectMake (- -, - -, -, -); //apply a random colorCGFloat red = (rand ()/(Double) Int_max); CGFloat Green= (rand ()/(Double) Int_max); CGFloat Blue= (rand ()/(Double) Int_max); Face.backgroundcolor= [Uicolor colorwithred:red green:green blue:blue Alpha:1.0]. Cgcolor; ?//apply the transform and returnFace.transform =transform;returnFace ;} -(Calayer *) Cubewithtransform: (catransform3d) transform{//Create cube LayerCatransformlayer *cube =[Catransformlayer layer];//add cube face 1Catransform3d ct = catransform3dmaketranslation (0,0, -); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //add cube face 2ct = catransform3dmaketranslation ( -,0,0); CT= Catransform3drotate (CT, m_pi_2,0,1,0); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //Add cube face 3ct = catransform3dmaketranslation (0, - -,0); CT= Catransform3drotate (CT, m_pi_2,1,0,0); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //Add cube face 4ct = catransform3dmaketranslation (0, -,0); CT= Catransform3drotate (CT,-m_pi_2,1,0,0); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //Add cube face 5ct = catransform3dmaketranslation (- -,0,0); CT= Catransform3drotate (CT,-m_pi_2,0,1,0); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //Add cube face 6ct = catransform3dmaketranslation (0,0, - -); CT= Catransform3drotate (CT, M_PI,0,1,0); [Cube addsublayer:[self FACEWITHTRANSFORM:CT]; //Center The cube layer within the containerCgsize containersize =self.containerView.bounds.size; Cube.position= Cgpointmake (Containersize.width/2.0, Containersize.height/2.0); //apply the transform and returnCube.transform =transform;returnCube;} - (void) viewdidload{? [Super Viewdidload]; //set up the perspective transformCatransform3d pt =catransform3didentity; PT.M34= -1.0/500.0; Self.containerView.layer.sublayerTransform=pt;//set up the transform for cube 1 and add itCatransform3d c1t =catransform3didentity; C1T= Catransform3dtranslate (c1t,- -,0,0); Calayer*CUBE1 =[self cubewithtransform:c1t]; [Self.containerView.layer ADDSUBLAYER:CUBE1]; //set up the transform for cube 2 and add itCatransform3d c2t =catransform3didentity; C2T= Catransform3dtranslate (c2t, -,0,0); C2T= Catransform3drotate (c2t,-m_pi_4,1,0,0); C2T= Catransform3drotate (c2t,-m_pi_4,0,1,0); Calayer*cube2 =[self cubewithtransform:c2t]; [Self.containerView.layer addsublayer:cube2];} @end
Fig. 6.5 Two different transformed cubes in the same view
Cagradientlayer
Cagradientlayer is used to generate two or more color smoothing gradients. It is also possible to copy a cagradientlayer with the core graphics and draw the content to a normal layer, but the real benefit of cagradientlayer is that it uses hardware acceleration.
Base gradient
We'll start with a simple red-to-blue diagonal gradient (see listing 6.6). These gradient colors are placed in an array and assigned to the Colors property. This array member accepts a value of type cgcolorref (not derived from nsobject), so we use bridge transformation to ensure that the compilation is normal.
Cagradientlayer also have StartPoint and endpoint properties, and they determine the direction of the gradient. These two parameters are defined in the unit coordinate system, so the upper-left coordinate is {0, 0}, and the lower-right coordinate is {1, 1}. Code Run result 6.6
Listing 6.6 A simple diagonal gradient of two colors
@interfaceViewcontroller () @property (nonatomic, weak) Iboutlet UIView*Containerview;@end @implementationViewcontroller- (void) viewdidload{[Super Viewdidload];//Create gradient layer and add it to our container viewCagradientlayer *gradientlayer =[Cagradientlayer layer]; Gradientlayer.frame=Self.containerView.bounds; [Self.containerView.layer Addsublayer:gradientlayer]; //Set Gradient ColorsGradientlayer.colors = @[(__bridgeID) [Uicolor Redcolor]. Cgcolor, (__bridgeID) [Uicolor Bluecolor]. Cgcolor]; //set gradient start and end pointsGradientlayer.startpoint = Cgpointmake (0,0); Gradientlayer.endpoint= Cgpointmake (1,1);} @end
Figure 6.6 A simple diagonal gradient for two colors with Cagradientlayer
[IOS animation]-calayer dedicated layer Catransformlayer