CALayer详解

来源:互联网 发布:星云数据服务平台 编辑:程序博客网 时间:2024/05/16 17:50

官方是这么定义的:

/** The base layer class. **/public class CALayer : NSObject, NSCoding, CAMediaTiming {

以下是对这个类的描述:

The CALayer class manages image-based content and allows you to perform animations on that content. 【CALayer这个类管理图形基础内容和允许你将动画表现在这内容上】Layers are often used to provide the backing store for views but can also be used without a view to display content. 【Layer被用作views的后备存储,但是同样可以在没有view的时候在content上展示动画】A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow.【一个layer的主要工作是管理你所提供的虚拟内容,同时这个layer它本身有虚拟属性,这些属性包括设置,例如,背景颜色,边框,阴影】 In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen. 【除了管理虚拟内容,layer同样维护它的关于几何学的内容】Modifying the properties of the layer is how you initiate animations on the layer’s content or geometry.【修改你初始化layer时内容和几何学的属性】 A layer object encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer’s timing information.【layer对象封装了动画的持续时间和步调,这种实现是其依赖于其继承了CAMediaTiming这个协议,这个协议是定义了layer的定时信息】

If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship.【如果layer对象是被view创建的,这个view可以分自动的分派layer的代理任务,而你不能改变这种关系】 For layers you create yourself, you can assign a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. 【对于你自己创建的layer,你能自己分派代理对象和使用对象提供的内容,这些内容包括layer动态地完成任务】A layer may also have a layout manager object (assigned to the layoutManager property) to manage the layout of subviews separately. 【layer同样拥有layout manager对象去管理子层的布局】

CALayers 是屏幕上的一个具有可见内容的矩形区域,每个UIView都有一个根CALayer,其所有的绘制(视觉效果)都是在这个layer上进行的。

UIView是iOS系统中界面元素的基础,所有的界面元素都继承自它。它本身完全是由CoreAnimation来实现的(Mac下似乎不是这样)。它真正的绘图部分,是由一个叫CALayer(Core Animation Layer)的类来管理。UIView本身,更像是一个CALayer的管理器,访问它的跟绘图和跟坐标有关的属性,例如frame,bounds等等,实际上内部都是在访问它所包含的CALayer的相关属性。

UIView有个layer属性,可以返回它的主CALayer实例,UIView有一个layerClass方法,返回主layer所使用的类,UIView的子类,可以通过重载这个方法,来让UIView使用不同的CALayer来显示。UIView之所以能够显示,完全是因为内部的CALayer对象。因此,通过操作这个CALayer对象,可以很方便地调整UIView的一些界面属性,比如:阴影、圆角大小、边框宽度和颜色等。

1.通过layer设置边框的宽度和颜色

view.layer.backgroundColor = UIColor .blueColor().CGColorview.layer.cornerRadius = 180view.layer.frame = CGRectMake(120, 350, 150, 30);

通过以上方式可以讲view添加圆角

public var allowsEdgeAntialiasing: Boolpublic var backgroundColor: CGColor?public var cornerRadius: CGFloatpublic var borderWidth: CGFloatpublic var borderColor: CGColor?public var opacity: Float...

以上是一些可以设置的属性,根据自己的需求进行设置

2、在layer上添加一张图片

view.layer.contents = UIImage.init(named: "2.jpg")?.CGImage

3.设置阴影

设置阴影,不光需要设置阴影颜色,还应该设置阴影的偏移位和透明度。因为如果不设置偏移位的话,那么阴影和layer完全重叠,且默认透明度为0(即完全透明)。

let sView = UIView()sView.frame = CGRectMake(100, 100, 100, 100)self.view.addSubview(sView)      sView.backgroundColor = UIColor.redColor()sView.layer.shadowColor = UIColor.blueColor().CGColorsView.layer.shadowOffset = CGSizeMake(12.0, 12.0)sView.layer.shadowOpacity = 0.6

注释:只要继承自uiview的都有layer属性

4、设置图片的形变属性(transform)

/* A transform applied to the layer relative to the anchor point of its* bounds rect. Defaults to the identity transform. Animatable. */public var transform: CATransform3D

transform这个属性是继承子CATransform3D
通过uiview设置(2D效果) 通过layer来设置(3D效果,x,y,z三个方向)

self.sView?.transform = CGAffineTransformMakeTranslation(0, -100)        self.sView!.layer.transform=CATransform3DMakeTranslation(100, 20, 100);

也可以通过KVC来实现

//通过KVC来设置NSValue *v=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(100, 20, 0)];[self.iconView.layer setValue:v forKeyPath:@"transform"];//如果是只需要设置在某一个方向上的移动,可以参考下面的代码//在x的方向上向左移动100[self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];

5、旋转一定的角度

self.sView!.layer.transform = CATransform3DMakeRotation(1.0, 1, 1, 0.5)

其他还有很多 有机会了再添加,写的很乱,还请见谅。

0 0
原创粉丝点击