CATransformLayer

作者: 莫须有恋 | 来源:发表于2016-12-29 10:43 被阅读33次

    好尴尬,按照套路,我点进了CATransformLayer.h,但是空空如也,什么也没有。那么CATransfromLayer的作用是啥呢?只能通过撸代码来体会了。

    //先上效果图吧


    对比效果吧

    部分代码

    绘制立方体
        //面1
        transfrom = CATransform3DMakeTranslation(0, 0, sub_width/2);
        [cube addSublayer:[self layerWithTransform:transfrom]];
        
        //面2
        transfrom = CATransform3DMakeTranslation(0, 0, -sub_width/2);
        [cube addSublayer:[self layerWithTransform:transfrom]];
        
        //面3
        transfrom = CATransform3DMakeTranslation(sub_width/2, 0, 0);
        transfrom = CATransform3DRotate(transfrom, M_PI_2, 0, 1, 0);
        [cube addSublayer:[self layerWithTransform:transfrom]];
        
        //面4
        transfrom = CATransform3DMakeTranslation(-sub_width/2, 0, 0);
        transfrom = CATransform3DRotate(transfrom, -M_PI_2, 0, 1, 0);
        [cube addSublayer:[self layerWithTransform:transfrom]];
        
        
        //面5
        transfrom = CATransform3DMakeTranslation(0, sub_width/2, 0);
        transfrom = CATransform3DRotate(transfrom, M_PI_2, 1, 0, 0);
        [cube addSublayer:[self layerWithTransform:transfrom]];
    
        //面6
        transfrom = CATransform3DMakeTranslation(0, -sub_width/2, 0);
        transfrom = CATransform3DRotate(transfrom, -M_PI_2, 1, 0, 0);
        [cube addSublayer:[self layerWithTransform:transfrom]];
    
    动画
        static float degree = 0.f;
        
        CGFloat duration = 1.f;
        CGFloat singleDuration = 1 / (duration * 60.0);
        CGFloat singleDegree = 45/(duration * 60.0);
        
        
        CATransform3D fromValue = CATransform3DIdentity;
        fromValue.m34 = -1/500;
        fromValue = CATransform3DRotate(fromValue, DEGREE(degree), 0, 1, 0);
        
        CATransform3D toValue = CATransform3DIdentity;
        toValue.m34 = -1/500;
        toValue = CATransform3DRotate(toValue, DEGREE(degree += singleDegree), 0, 1, 0);
        
        //使用transform无法实现旋转2*M_PI,此时可以使用虚拟属性@"transform.rotation.y",可以使用相对值旋转byValue
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        animation.duration = singleDuration;
        animation.fromValue = [NSValue valueWithCATransform3D:fromValue];
        animation.toValue = [NSValue valueWithCATransform3D:toValue];
        animation.repeatCount = HUGE_VALL;
        animation.removedOnCompletion = NO;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        [layer addAnimation:animation forKey:@"transform"];
    

    结论:CATransformLayer可以为他的子图层,统一提供景深效果。(如有错误,请指正。如需源码,请点赞留言)

    相关文章

      网友评论

      • 1cdb19c230be:希望下次能写的全一些 我看了你的代码后 自己试着写了一下 很多地方断的 :sweat:

      本文标题:CATransformLayer

      本文链接:https://www.haomeiwen.com/subject/eowyvttx.html