美文网首页
CALayer图层类

CALayer图层类

作者: nothing_c | 来源:发表于2016-10-30 23:16 被阅读24次

    //CALayer图层类

    //和UIView用法类似

    //创建图层类对象

    //    CALayer *layer = [[CALayer alloc] init];

    //    MyColorRef color = [UIColor cyanColor];

    //    layer.backgroundColor = color.CGColor;

    //    layer.frame = CGRectMake(100, 100, 100, 100);

    //    [self.view.layer addSublayer:layer];

    //基本图层

    CALayer *myLayer = [CALayer layer];

    myLayer.backgroundColor= [UIColor orangeColor].CGColor;

    myLayer.frame=CGRectMake(100, 100, 100, 100);

    //边框

    {

    //边框宽度

    myLayer.borderWidth = 5;

    //边框颜色

    myLayer.borderColor= [UIColor greenColor].CGColor;

    }

    //阴影

    {

    //颜色

    myLayer.shadowColor= [UIColor redColor].CGColor;

    //阴影偏移

    myLayer.shadowOffset = CGSizeMake(1, 1);

    //圆角

    myLayer.shadowRadius = 10;

    //图透明度

    myLayer.shadowOpacity = 0.7;

    }

    //阴影路径

    {

    CGMutablePathRef path = CGPathCreateMutable();

    const CGAffineTransform transform = CGAffineTransformIdentity;

    CGPathAddEllipseInRect(path, &transform,CGRectMake(-5, -5, 110, 110));

    myLayer.shadowPath= path;

    //释放

    CGPathRelease(path);

    }

    //圆角半径

    myLayer.cornerRadius = 50;

    //不透明度浑浊

    myLayer.opacity= 0.6;

    //溢出(在边框范围内显示)阴影消失

    myLayer.masksToBounds = YES;

    //位置默认的position是自身的中心点

    CGPoint position = myLayer.position;

    NSLog(@"%@",NSStringFromCGPoint(position));

    //往左上移动

    //    myLayer.position = CGPointMake(100, 100);

    //锚点

    CGPoint anchorPoint = myLayer.anchorPoint;

    NSLog(@"%@",NSStringFromCGPoint(anchorPoint));

    myLayer.anchorPoint=CGPointMake(0, 0);

    //求在父视图中的坐标点(origin)

    //    x = position.x - anchorPoint.x * self.width

    //    y = position.y - anchorPoint.y * self.height

    //内容

    {

    UIImage *image =[UIImage imageNamed:@"8.jpg"];

    myLayer.contents= (id)image.CGImage;

    }

    //添加到父图层上

    [self.view.layer addSublayer:myLayer];

    //文字图层

    CATextLayer *textLayer = [CATextLayer layer];

    textLayer.frame = CGRectMake(40, 300, 200, 200);

    textLayer.backgroundColor = [UIColor cyanColor].CGColor;

    textLayer.string = @"这是一个文字图层这是一个文字图层这是一个文字图层这是一个文字图层这是一个文字图层";

    //前景色(文字的颜色)

    textLayer.foregroundColor = [UIColor redColor].CGColor;

    //对齐

    textLayer.alignmentMode = kCAAlignmentCenter;

    //换行注意高度

    textLayer.wrapped=YES;

    //设置字体大小

    textLayer.fontSize = 24;

    [self.view.layer addSublayer:textLayer];

    //渐变图层

    CAGradientLayer *gradientLayer = [CAGradientLayerlayer];

    gradientLayer.frame = self.view.frame;

    //渐变颜色数组

    //.CGColor必须写不然不显色

    gradientLayer.colors= @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];

    //从什么地方开始变色渐变的位置一段一段

    gradientLayer.locations = @[@0.3,@0.6,@0.8];

    //kCAGradientLayerAxial线性渐变

    gradientLayer.type = kCAGradientLayerAxial;

    //渐变左上往右下0--1

    //开始点

    gradientLayer.startPoint = CGPointMake(0,0);

    //结束点

    gradientLayer.endPoint = CGPointMake(1,1);

    //[self.view.layer addSublayer:gradientLayer];

    //插入图层层次

    [self.view.layer insertSublayer:gradientLayer below:myLayer];

    相关文章

      网友评论

          本文标题:CALayer图层类

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