CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的。
CALayer外形特征主要包括:
1、层的大小尺寸
2、背景色
3、内容(可以填充图片或者使用Core Graphics绘制的内容)
4、矩形是否使用圆角
5、矩形是否有阴影
Layer有很多种,最常用也是最基本的是CALayer,当然还包括其他的子类:
CAScrollerLayer 简化显示层的一部分
CATextLayer 文本层
CAGradientLayer、CAShapeLayer等等
使用层之前,需要在项目中引入QuartzCore.framework框架
实例:
新建XCode项目LayerSample,导入QuartzCore.famework
在LayerSampleViewController.h文件中引入:
#import <QuartzCore/QuartzCore.h>
</pre><pre class="csharpcode" name="code" style="white-space: pre-wrap; word-wrap: break-word; line-height: 26px; margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 13px; font-family: consolas, 'Courier New', courier, monospace; background-color: rgb(255, 255, 255);"><span style="color: rgb(100, 107, 134); margin: 0px; padding: 0px;">LayerSampleViewController.m文件,代码:</span>
#import <span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 96, 128);">"LayerSampleViewController.h"</span>
@implementation LayerSampleViewController
- (<span class="kwrd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 255);">void</span>)loadView {
UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
rootView.backgroundColor = [UIColor whiteColor];
self.view = rootView;
[rootView release];
<span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//加载layer</span>
backLayer = [CALayer layer];
backLayer.backgroundColor = [UIColor orangeColor].CGColor;
backLayer.bounds = CGRectMake(10, 10, 1004, 728); <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置layer的区域</span>
backLayer.position = CGPointMake(1024/2, 768/2-10); <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置layer坐标</span>
[self.view.layer addSublayer:backLayer];
}
- (<span class="kwrd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 255);">void</span>)dealloc {
[super dealloc];
}
@end
运行程序:
为了区分背景UIView,我将UIView的背景色设置为白色,backLayer的背景色设置为橘黄色。
上图是一个矩形区域,如果设置矩形的四个角是椭圆角,可以添加属性:
backLayer.cornerRadius = 20.0; //矩形椭圆角的弧度半径
运行程序后,效果如图:
添加带阴影的子Layer(层),代码:
<span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);"> //添加子layer</span>
CALayer *cyanLayer = [CALayer layer];
cyanLayer.backgroundColor = [UIColor cyanColor].CGColor;
cyanLayer.bounds = CGRectMake(0, 0, 300, 300);
cyanLayer.position = CGPointMake(180, 180);
cyanLayer.shadowOffset = CGSizeMake(0, 3); <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的偏移量</span>
cyanLayer.shadowRadius = 10.0; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的半径</span>
cyanLayer.shadowColor = [UIColor blackColor].CGColor; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的颜色为黑色</span>
cyanLayer.shadowOpacity = 0.9; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的不透明度</span>
[backLayer addSublayer:cyanLayer];
运行效果图:
给子Layer添加图片
//添加子image layer
UIImage *image = [UIImage imageNamed:<span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 96, 128);">@"feiche.jpg"</span>];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(100, 100, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.shadowOffset = CGSizeMake(0, 3); <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的偏移量</span>
imageLayer.shadowRadius = 10.0; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的半径</span>
imageLayer.shadowColor = [UIColor blackColor].CGColor; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的颜色为黑色</span>
imageLayer.shadowOpacity = 0.9; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的不透明度</span>
[backLayer addSublayer:imageLayer];
运行效果,
图片加边框,代码:
imageLayer.borderColor = [UIColor grayColor].CGColor; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//边框颜色</span>
imageLayer.borderWidth = 2.0; //边框宽度
运行效果:
设置图片为椭圆角,代码:
imageLayer.cornerRadius = 10.0; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置layer圆角半径</span>
imageLayer.masksToBounds = YES; //隐藏边界
运行效果:
但是由于设置masksToBounds 属性为true所以Layer的阴影效果也就没有了。
之前我都是将图片另外在做有阴影效果的图片,这样显的比较真实,如果阴影部分只是填充边框即可,可以采用两个Layer来实现阴影效果,代码:
UIImage *image = [UIImage imageNamed:<span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 96, 128);">@"feiche.jpg"</span>];
<span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//阴影layer</span>
CALayer *shadowLayer = [CALayer layer];
shadowLayer.frame = CGRectMake(100, 100, image.size.width, image.size.height);
shadowLayer.backgroundColor = [UIColor blueColor].CGColor;
shadowLayer.shadowOffset = CGSizeMake(0, 3);
shadowLayer.cornerRadius = 10.0;
shadowLayer.shadowRadius = 10.0;
shadowLayer.shadowColor = [UIColor blackColor].CGColor; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的颜色为黑色</span>
shadowLayer.shadowOpacity = 1.0; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置阴影的不透明度</span>
[backLayer addSublayer:shadowLayer];
<span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//添加子image layer</span>
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(100, 100, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.cornerRadius = 10.0; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//设置layer圆角半径</span>
imageLayer.masksToBounds = YES; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//隐藏边界</span>
imageLayer.borderColor = [UIColor grayColor].CGColor; <span class="rem" style="margin: 0px; padding: 0px; color: rgb(0, 128, 0);">//边框颜色</span>
imageLayer.borderWidth = 2.0;
[backLayer addSublayer:imageLayer];
运行后效果: