We said before we can use Calayer collocation catransform3d to achieve the view to do 3D rotation, today we look at a 3D new things
Catransformlayer, look at the name to know that the layer is related to rotation, then what is the specific?
We look at his head file, without any attributes. He's actually as a container, and we can add other calayer to it.
For example, we're going to create a cube with a 3D effect,
You can create a Catransformlayer container first,
The 6 sides of the cube are represented by a layer of rotated layers, added to the container, adjusted to the angle and position of each face so that they can be stitched together and then a cube.
Let's get the code.
- (void) viewdidload {[Super viewdidload]; //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.view.bounds.size; Cube.position= Cgpointmake (Containersize.width/2.0, Containersize.height/2.0); Cube.transform= Catransform3dmakerotation ( -,1,1,0); [Self.view.layer Addsublayer:cube];}-(Calayer *) Facewithtransform: (catransform3d) transform{//Create cube face layerCalayer *face =[Calayer layer]; Face.bounds= CGRectMake (0,0, -, -); //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; Face.transform=transform; returnFace ;}@end
We can give the container a spinning animation, and we'll implement a spinning cube.
Catransform3d Transa = catransform3dmakerotation (111); *animation = [cabasicanimation animationwithkeypath:@ "transform"]; Animation.duration 2; animation.autoreverses = YES; Animation.repeatcount ; Animation.tovalue = [Nsvalue Valuewithcatransform3d:transa]; [Cube addanimation:animation Forkey:nil];
Making 3D images and animations using Catransformlayer