Making 3D images and animations using Catransformlayer

Source: Internet
Author: User

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

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.