Core Animation笔记(特殊图层)

1.shapeLayer: 渲染快速,内存占用小,不会被图层边界裁掉(可以在边界之外绘制),不会像素化(当做3D变化如缩放是不会失真)

   CGRect rect = self.containerView.bounds;
    
    CAShapeLayer *shaper = [CAShapeLayer new];
    shaper.backgroundColor = [UIColor yellowColor].CGColor;
    shaper.lineWidth = 2;
    shaper.strokeColor = [UIColor redColor].CGColor;
    //单独设置左上,右上圆角
    UIRectCorner rcorner = UIRectCornerTopLeft|UIRectCornerTopRight;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:rcorner cornerRadii:CGSizeMake(30, 30)];
    //设置路径
    shaper.path = path.CGPath;
//    [self.containerView.layer addSublayer:shaper];
    
    //定义父图层的可见区域(这里使父图层的圆角单独设置左上,右上)
    self.containerView.layer.mask = shaper;

2.CATextLayer

     CATextLayer *textLayer = [CATextLayer new];
    textLayer.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer:textLayer];
    
    //设置文本属性
    textLayer.foregroundColor = [UIColor blueColor].CGColor;
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.wrapped = YES;
    
    //设置字体
    UIFont *font = [UIFont systemFontOfSize:14];
    CFStringRef fontStrRef = (__bridge CFStringRef)font.fontName;
    CGFontRef fontRef = CGFontCreateWithFontName(fontStrRef);
    textLayer.font = fontRef;
    CFRelease(fontRef);
    textLayer.fontSize = font.pointSize;
    //设置缩放倍数,防止像素化
    textLayer.contentsScale = [UIScreen mainScreen].scale;
    //这里也可以设置富文本字符串NSMutableString
    textLayer.string = @"on my god we you yao bei rangyizhuier le ma ,zhe me zhen shi de ma ,ai ";

3.CATransformLayer: 不平面化子图层,用于构建3D 形状,如下代码用于构建一个正方体

//产生某个面的子layer
- (CALayer*)faceWithTransform:(CATransform3D)transform{
    CALayer *layer = [CALayer layer];
    
    layer.frame = CGRectMake(-50, -50, 100, 100);
    
    layer.backgroundColor = [UIColor colorWithRed:rand()/(double)INT_MAX green:rand()/(double)INT_MAX  blue:rand()/(double)INT_MAX alpha:1].CGColor;
    layer.transform = transform;
    
    return layer;
}
- (CALayer*)cubeWithTransform:(CATransform3D)transform{
    CATransformLayer *cubeLayer = [CATransformLayer layer];
    //1
    CATransform3D ct = CATransform3DMakeTranslation(0, 0, 50);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    
    //2
    ct = CATransform3DMakeTranslation(50, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    
    //3
    ct = CATransform3DMakeTranslation(0, -50, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    //4
    ct = CATransform3DMakeTranslation(0, 50, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    //5
    ct = CATransform3DMakeTranslation(-50, 0, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 0, 1, 0);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    //6
    ct = CATransform3DMakeTranslation(0, 0, -50);
    ct = CATransform3DRotate(ct, M_PI, 0, 1, 0);
    [cubeLayer addSublayer:[self faceWithTransform:ct]];
    cubeLayer.position = CGPointMake(self.containerView.bounds.size.width/2, self.containerView.bounds.size.height/2);

    //运用3D变换
    cubeLayer.transform = transform;

    return cubeLayer;

} 
- (void)testTransformLayer{
    CATransform3D tran = CATransform3DIdentity;
    tran.m34 = -1.0/500;
//添加透视效果
    self.containerView.layer.sublayerTransform = tran;
    
    CATransform3D ct2  = CATransform3DIdentity;
    ct2 = CATransform3DTranslate(ct2, 100, 0, 0);
    ct2 = CATransform3DRotate(ct2, -M_PI_4, 1, 0, 0);
    ct2 = CATransform3DRotate(ct2, -M_PI_4, 0, 1, 0);

    [self.containerView.layer addSublayer:[self cubeWithTransform:ct2]];
    
}

 

4.CAGradientLayer:渐变图层

 CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer:gradientLayer];
    
    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor greenColor].CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    //各个渐变点开始渐变的位置,和colors数组个数必须一致.
    gradientLayer.locations = @[@0.0,@0.1,@0.2]

5.CAReplicatorLayer,生成重复的子图层,并未每个图层应用变换

    CAReplicatorLayer *replayer = [CAReplicatorLayer layer];
    replayer.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer:replayer];
//复制个数
    replayer.instanceCount = 10;
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, 0, 200, 0);
    transform = CATransform3DRotate(transform, M_PI/5.0, 0, 0, 1);
    transform = CATransform3DTranslate(transform, 0, -200, 0);
    //运用变换
    replayer.instanceTransform = transform;
    
    //改变下一图层的蓝红通道 ,每个复制的图层颜色不一样
    replayer.instanceBlueOffset = -0.1;
    replayer.instanceRedOffset = - 0.1;
    
//放一个子图层进去
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(100, 100, 100, 100);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [replayer addSublayer:layer];

产生一个倒影效果(这里自定义一个view用于产生倒影效果):

自定义view,返回指定的图层
+ (Class)layerClass{
    return [CAReplicatorLayer class];
}
- (instancetype)init{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUp];

    }
    return self;
}
- (void)awakeFromNib{
    [super awakeFromNib];
    [self setUp];
}
- (void)setUp{
    CAReplicatorLayer *layer = (CAReplicatorLayer*)self.layer;
    layer.instanceCount = 2;
    
    CATransform3D transform = CATransform3DIdentity;
   
    CGSize size = self.bounds.size;
    transform = CATransform3DTranslate(transform, 0, size.height+2, 0);
    
    /**
        y方向倒转
     */
    transform = CATransform3DScale(transform, 1, -1, 0);
    layer.instanceTransform = transform;
    layer.instanceAlphaOffset = -0.6;
    
}

6.CAEmitterLayer:粒子发散效果

 CAEmitterLayer *emmitLayer = [CAEmitterLayer layer];
    emmitLayer.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer:emmitLayer];
    CGSize size = self.containerView.frame.size;
    
    emmitLayer.masksToBounds = true;
    //重叠部分效果,kCAEmitterLayerAdditive,重叠部分更亮
    emmitLayer.renderMode = kCAEmitterLayerAdditive;
    //发散点
    emmitLayer.emitterPosition = CGPointMake(size.width/2, size.height/2);
    
    CAEmitterCell *cell =[ CAEmitterCell emitterCell];
//注意图片大小
    cell.contents = (__bridge id)[UIImage imageNamed:@"spark.jpg"].CGImage;
    cell.birthRate = 50;//每秒多少个
    cell.lifetime = 5;
    
    cell.color = [UIColor colorWithRed:1 green:0.5 blue:1 alpha:1].CGColor;
    cell.alphaSpeed = -0.3;
    cell.velocity = 150;
    cell.velocityRange = 150;
//发散范围
    cell.emissionRange = M_PI*2;
    
    emmitLayer.emitterCells = @[cell];

7.其他

CAEAGLLayer

CAScrollLayer

CATiledLayer:载入大图

 

posted @ 2019-04-13 20:51  菜鸟工程司  阅读(222)  评论(0编辑  收藏  举报