一些简单的操做,供初学者参考
/*
* CALayer 在 QuartzCore 框架中
* CGColorRef 和 CGImageRef 在 CoreGraphics 框架中
* UIColor 和 UIImage 在UIKit框架中
* QuartzCore,CoreGraphics跨平台使用Mac和OS 都可以用,UIKit仅限在OS中使用
*/
CALayer *layer = [[CALayer alloc]init];//新建layer
layer.backgroundColor = [UIColor redColor].CGColor;//设置layer背景色
layer.bounds = CGRectMake(0, 0, 200, 200);
layer.position = CGPointMake(self.window.center.x, self.window.center.y);
//阴影
layer.shadowColor = [UIColor yellowColor].CGColor;//设置shadow的color
layer.shadowOffset = CGSizeMake(10.0, 10.0);//偏移量
layer.shadowOpacity = 1.0;//透明度
//圆角
layer.cornerRadius = 100;
[self.window.layer addSublayer:layer];
//边框
layer.borderWidth = 2.0;
layer.borderColor = [UIColor greenColor].CGColor;
//添加内容
layer.masksToBounds = YES;//
UIImage *image = [UIImage imageNamed:@"psb.jpg"];
layer.contents = (id)image.CGImage;
CALayer *layer = [[CALayer alloc]init];//新建layer
layer.backgroundColor = [UIColor redColor].CGColor;//设置layer背景色
layer.bounds = CGRectMake(0, 0, 200, 200);
//anchorPoint(锚点)和position关系
//position是layer中锚点super layer(父层)上面的坐标
//anchorPoint(锚点)和position共同控制layer在父层上的位置,anchorPoint默认值为(0.5,0.5)即锚点为layer的中心点
layer.position = CGPointMake(160, 240);
layer.anchorPoint = CGPointMake(0.5, 0.75);
渐变层,可以产生颜色渐变的效果
//渐变层
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
gradientLayer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor greenColor].CGColor];
gradientLayer.frame = CGRectMake(10, 20, 200, 200);
//开始点
gradientLayer.startPoint = CGPointMake(0.5, 0);
//结束点
gradientLayer.endPoint = CGPointMake(0.5, 1.0);
[self.window.layer addSublayer:gradientLayer];