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.CATextLayerapp

     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:漸變圖層ui

 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,生成重複的子圖層,並未每一個圖層應用變換spa

    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用於產生倒影效果):code

自定義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:粒子發散效果orm

 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.其餘blog

CAEAGLLayer圖片

CAScrollLayer內存

CATiledLayer:載入大圖

相關文章
相關標籤/搜索