Core Animation - CATransformLayer的运用

简介: Core Animation - CATransformLayer的运用

在做3D事物的时候,我们都需要做出独立的3D效果,在前面的博客中,我们画过一个3D的立方体,但是它却并不是独立的,而是由放在主视图上的6张子视图通过平移旋转得到的,这里通过CATransformLayer我们可以得到完全独立的3D图形,可以直接对整个3D事物进行一些操作。需要说明的是CATransformLayer并不显示自身的内容,只有当存在了一个作用域子图层时才会显示子图层的变换,CATransformLayer才真正存在,类似于一个画布。

- (CALayer *)faceWithTransform:(CATransform3D)transform
{
    //create cube face layer
    CALayer *face = [CALayer layer];
    face.frame = CGRectMake(-50, -50, 100, 100);
    //apply a random color
    CGFloat 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;
    return face;
}
- (CALayer *)cubeWithTransform:(CATransform3D)transform
{
    //create cube layer
    CATransformLayer *cube = [CATransformLayer layer];
    //add cube face 1
    CATransform3D ct = CATransform3DMakeTranslation(0, 0, 50);
    [cube addSublayer:[self faceWithTransform:ct]];
    //add cube face 2
    ct = CATransform3DMakeTranslation(50, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    //add cube face 3
    ct = CATransform3DMakeTranslation(0, -50, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    //add cube face 4
    ct = CATransform3DMakeTranslation(0, 50, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    //add cube face 5
    ct = CATransform3DMakeTranslation(-50, 0, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    //add cube face 6
    ct = CATransform3DMakeTranslation(0, 0, -50);
    ct = CATransform3DRotate(ct, M_PI, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    //center the cube layer within the container
    CGSize containerSize = self.view.bounds.size;
    cube.position = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
    //apply the transform and return
    cube.transform = transform;
    return cube;
}
 CATransform3D pt = CATransform3DIdentity;
    pt.m34 = -1.0 / 500.0;
    self.view.layer.sublayerTransform = pt;
    //set up the transform for cube 1 and add it
    CATransform3D c1t = CATransform3DIdentity;
    c1t = CATransform3DTranslate(c1t, 0, -100, 0);
    CALayer *cube1 = [self cubeWithTransform:c1t];
    [self.view.layer addSublayer:cube1];
    CATransform3D c2t = CATransform3DIdentity;
    c2t = CATransform3DTranslate(c2t, 0, 100, 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.view.layer addSublayer:cube2];

记得以前说过m34么?还有CATransform3D。这些东西平日里用的很少,不需要直接就能写出来,只需要知道功能即可,当用到的时候就去找,然后复制过来改改。如果以后从事跟这个有关的,相信熟能生巧,惯例,附上github下载地址:https://github.com/codeliu6572/CATransformLayer

目录
相关文章
|
图形学
Core Animation -关键帧动画
Core Animation -关键帧动画
81 0
Core Animation -CGContextRef的运用,还有详细解释
Core Animation -CGContextRef的运用,还有详细解释
72 0
Core Animation - 渐变色CAGradientLayer
Core Animation - 渐变色CAGradientLayer
73 0
Core Animation - 图层行为
Core Animation - 图层行为
58 0
Core Animation - 图层行为
|
iOS开发
Core Animation - 图层几何学<一>
Core Animation - 图层几何学<一>
73 0
Core Animation - 图层几何学<一>
|
iOS开发 MacOS
Core Animation - 图层几何学<二>
Core Animation - 图层几何学<二>
66 0
|
图形学
Core Animation - CATextLayer和富文本
Core Animation - CATextLayer和富文本
76 0
Core Animation - 事务之隐式动画
Core Animation - 事务之隐式动画
54 0
Core Animation - 变换<五>
Core Animation - 变换<五>
44 0
Core Animation - 变换<二>
Core Animation - 变换<二>
59 0