首页 > 代码库 > CALayer
CALayer
// view的完整显示过程// 1. view.layer会准备一个Layer Graphics Contex(图层类型的上下文)
// 2. 调用view.layer.delegate(view)的drawLayer:inContext:,并传入刚才准备好的上下文
// 3. view的drawLayer:inContext:方法内部又会调用view的drawRect:方法
// 4. view就可以在drawRect:方法中实现绘图代码, 所有东西最终都绘制到view.layer上面
// 5. 系统再将view.layer的内容拷贝到屏幕, 于是完成了view的显示
#pragma mark - 图层的代理方法
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20));
CGContextFillPath(ctx);
}
- (void)diyLayer
{
MJLayer *layer = [MJLayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.backgroundColor = [UIColor blueColor].CGColor;
layer.anchorPoint = CGPointZero;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
}
显示调用Layer的setNeedsDisplay方法,会调用Layer的以下方法进行绘制
- (void)drawInContext:(CGContextRef)ctx
{
// 红色
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
// 添加圆
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
// 实心绘制
CGContextFillPath(ctx);
}
显示调用View的setNeedsDisplay方法,会调用View的以下方法进行绘制
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 红色
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
// 添加圆
CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));
// 实心绘制
CGContextFillPath(ctx);
}
1.1->Makeself.iconView.layer.transform = CATransform3DMakeScale(1.5, 0.5, 0);
self.iconView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
self.iconView.layer.transform = CATransform3DMakeTranslation(1, 1, 1);
self.iconView.layer.transform = CATransform3DMakeAffineTransform(self.iconView.transform);
1.2 -> No Make
self.iconView.layer.transform = CATransform3DRotate(self.iconView.layer.transform, 1, 1, 1, 1);
self.iconView.layer.transform = CATransform3DScale(self.iconView.layer.transform, 1, 1, 1);
self.iconView.layer.transform = CATransform3DTranslate(self.iconView.layer.transform, 1, 1, 1);
1.3 ->KVC(同时设置多个Layer的transform属性)
1.3.1
self.iconView.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 1, 1);
1.3.2
NSValue *value = http://www.mamicode.com/[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
[self.iconView.layer setValue:value forKeyPath:@“transform"];
1.3.3
[self.iconView.layer setValue:@(M_PI_2) forKeyPath:@"transform.rotation"];
2.1->Makeself.iconView.transform = CGAffineTransformMakeRotation(1);self.iconView.transform = CGAffineTransformMakeScale(1, 1);
self.iconView.transform = CGAffineTransformMakeTranslation(1, 1);
//CGAffineTransformMake是一个宏(struct CGAffineTransform)
self.iconView.transform = CGAffineTransformMake(1, 1, 1, 1, 1, 1);
2.2->No Make
self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, 1);
self.iconView.transform = CGAffineTransformScale(self.iconView.transform, 1, 1);
self.iconView.transform = CGAffineTransformTranslate(self.iconView.transform, 1, 1);
3、Layer属性(如果头文件里面属性是Animatable,则设置值时是有动画的,这叫隐式动画)
3.0创建Layer
CALayer *layer = [CALayer layer];
3.1->边框宽度
self.iconView.layer.borderWidth = 10;
3.2->边框颜色
self.iconView.layer.borderColor = [UIColor greenColor].CGColor;
3.3->圆角
self.iconView.layer.cornerRadius = 10;
3.4->超出主层边框范围的内容都剪掉self.iconView.layer.masksToBounds = YES;
self.iconView.clipsToBounds = YES;
3.5->阴影颜色
self.iconView.layer.shadowColor = [UIColor blueColor].CGColor;
3.6->阴影偏差
self.iconView.layer.shadowOffset = CGSizeMake(20, 20);
3.7->阴影不透明度
self.iconView.layer.shadowOpacity = 0.5;
3.8->位置属性(默认是在中心点)
layer.position = CGPointZero;
3.9->锚点属性(设置position是指的Layer的哪个位置)
layer.anchorPoint = CGPointZero;
CALayer