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>
- LayerSampleViewController.m文件,代码:
- //
- // LayerSampleViewController.m
- // LayerSample
- //
- // Created by jordy on 11-3-28.
- // Copyright 2011 vsp. All rights reserved.
- //
-
- #import "LayerSampleViewController.h"
-
- @implementation LayerSampleViewController
-
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
- rootView.backgroundColor = [UIColor whiteColor];
- self.view = rootView;
- [rootView release];
-
- //加载layer
-
- backLayer = [CALayer layer];
- backLayer.backgroundColor = [UIColor orangeColor].CGColor;
- backLayer.bounds = CGRectMake(10, 10, 1004, 728); //设置layer的区域
- backLayer.position = CGPointMake(1024/2, 768/2-10); //设置layer坐标
- [self.view.layer addSublayer:backLayer];
-
- }
-
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
- }
-
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
- }
-
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
-
- - (void)dealloc {
- [super dealloc];
- }
-
- @end
- #import <QuartzCore/QuartzCore.h>
- LayerSampleViewController.m文件,代码:
- //
- // LayerSampleViewController.m
- // LayerSample
- //
- // Created by jordy on 11-3-28.
- // Copyright 2011 vsp. All rights reserved.
- //
-
- #import "LayerSampleViewController.h"
-
- @implementation LayerSampleViewController
-
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
- rootView.backgroundColor = [UIColor whiteColor];
- self.view = rootView;
- [rootView release];
-
- //加载layer
-
- backLayer = [CALayer layer];
- backLayer.backgroundColor = [UIColor orangeColor].CGColor;
- backLayer.bounds = CGRectMake(10, 10, 1004, 728); //设置layer的区域
- backLayer.position = CGPointMake(1024/2, 768/2-10); //设置layer坐标
- [self.view.layer addSublayer:backLayer];
-
- }
-
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
- }
-
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
- }
-
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
-
- - (void)dealloc {
- [super dealloc];
- }
-
- @end
运行程序:
为了区分背景UIView,我将UIView的背景色设置为白色,backLayer的背景色设置为橘黄色。
上图是一个矩形区域,如果设置矩形的四个角是椭圆角,可以添加属性:
backLayer.cornerRadius = 20.0; //矩形椭圆角的弧度半径
运行程序后,效果如图:
添加带阴影的子Layer(层),代码:
- //添加子layer
- 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); //设置阴影的偏移量
- cyanLayer.shadowRadius = 10.0; //设置阴影的半径
- cyanLayer.shadowColor = [UIColor blackColor].CGColor; //设置阴影的颜色为黑色
- cyanLayer.shadowOpacity = 0.9; //设置阴影的不透明度
-
- [backLayer addSublayer:cyanLayer];
- //添加子layer
- 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); //设置阴影的偏移量
- cyanLayer.shadowRadius = 10.0; //设置阴影的半径
- cyanLayer.shadowColor = [UIColor blackColor].CGColor; //设置阴影的颜色为黑色
- cyanLayer.shadowOpacity = 0.9; //设置阴影的不透明度
-
- [backLayer addSublayer:cyanLayer];
运行效果图:
给子Layer添加图片
- //添加子image layer
- UIImage *image = [UIImage imageNamed:@"feiche.jpg"];
- 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); //设置阴影的偏移量
- imageLayer.shadowRadius = 10.0; //设置阴影的半径
- imageLayer.shadowColor = [UIColor blackColor].CGColor; //设置阴影的颜色为黑色
- imageLayer.shadowOpacity = 0.9; //设置阴影的不透明度
- [backLayer addSublayer:imageLayer];
- //添加子image layer
- UIImage *image = [UIImage imageNamed:@"feiche.jpg"];
- 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); //设置阴影的偏移量
- imageLayer.shadowRadius = 10.0; //设置阴影的半径
- imageLayer.shadowColor = [UIColor blackColor].CGColor; //设置阴影的颜色为黑色
- imageLayer.shadowOpacity = 0.9; //设置阴影的不透明度
- [backLayer addSublayer:imageLayer];
运行效果,
图片加边框,代码:
- imageLayer.borderColor = [UIColor grayColor].CGColor; //边框颜色
- imageLayer.borderWidth = 2.0; //边框宽度
- imageLayer.borderColor = [UIColor grayColor].CGColor; //边框颜色
- imageLayer.borderWidth = 2.0; //边框宽度
运行效果:
设置图片为椭圆角,代码:
- imageLayer.cornerRadius = 10.0; //设置layer圆角半径
- imageLayer.masksToBounds = YES; //隐藏边界
- imageLayer.cornerRadius = 10.0; //设置layer圆角半径
- imageLayer.masksToBounds = YES; //隐藏边界
运行效果:
但是由于设置masksToBounds 属性为true所以Layer的阴影效果也就没有了。
之前我都是将图片另外在做有阴影效果的图片,这样显的比较真实,如果阴影部分只是填充边框即可,可以采用两个Layer来实现阴影效果,代码:
- UIImage *image = [UIImage imageNamed:@"feiche.jpg"];
- //阴影layer
- 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; //设置阴影的颜色为黑色
- shadowLayer.shadowOpacity = 1.0; //设置阴影的不透明度
- [backLayer addSublayer:shadowLayer];
-
- //添加子image layer
- CALayer *imageLayer = [CALayer layer];
- imageLayer.frame = CGRectMake(100, 100, image.size.width, image.size.height);
- imageLayer.contents = (id) image.CGImage;
- imageLayer.cornerRadius = 10.0; //设置layer圆角半径
- imageLayer.masksToBounds = YES; //隐藏边界
- imageLayer.borderColor = [UIColor grayColor].CGColor; //边框颜色
- imageLayer.borderWidth = 2.0;
- [backLayer addSublayer:imageLayer];
- UIImage *image = [UIImage imageNamed:@"feiche.jpg"];
- //阴影layer
- 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; //设置阴影的颜色为黑色
- shadowLayer.shadowOpacity = 1.0; //设置阴影的不透明度
- [backLayer addSublayer:shadowLayer];
-
- //添加子image layer
- CALayer *imageLayer = [CALayer layer];
- imageLayer.frame = CGRectMake(100, 100, image.size.width, image.size.height);
- imageLayer.contents = (id) image.CGImage;
- imageLayer.cornerRadius = 10.0; //设置layer圆角半径
- imageLayer.masksToBounds = YES; //隐藏边界
- imageLayer.borderColor = [UIColor grayColor].CGColor; //边框颜色
- imageLayer.borderWidth = 2.0;
- [backLayer addSublayer:imageLayer];
运行后效果:
------转自http://blog.csdn.net/linzhiji/article/details/6748207