持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第14天,点击查看活动详情
概要
- CALayer和UIVIew的关系
-
CALayer 负责视图中的内容和动画--不会改变CALayer的真实属性
-
UIVIew 负责监听和响应事件
- CALayer的position、anchorPoint属性的作用
- 核心动画基本概念
- 基本动画
- 关键帧动画
- 动画组
- 转场动画
I CALayer
在创建一个UIView对象时,UIView内部会自动创建一个图层(即CALayer对象);通过UIView对象的layer属性可以访问这个层,通常称这个层为RootLayer。
-
当UIView需要显示到屏幕的时候,会调用
drawRect:
方法进行绘图,并且将所有的内容绘制到自己的图层上,绘制完毕之后,系统会将图层拷贝到屏幕上,于是完成了UIView的显示。 -
CALayer的基本使用: 通过操作CALayer对象,可以很方便地调整UIView的一些外观属性(阴影、圆角大小、边框宽度以及颜色);还可以给图层添加动画,实现一些比较酷炫的效果。
-
CALayer的属性
/* The bounds of the layer. Defaults to CGRectZero. Animatable. 宽度和高度 */
@property CGRect bounds;
/* The position in the superlayer that the anchor point of the layer's * bounds rect is aligned to. Defaults to the zero point. Animatable. 位置,默认指向终点,用来设置CALayer在父层中的位置,以父层的左上角为原点(0,0) */
@property CGPoint position;
/* Defines the anchor point of the layer's bounds rect, as a point in * normalized layer coordinates - '(0, 0)' is the bottom left corner of
* the bounds rect, '(1, 1)' is the top right corner. Defaults to * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. 称为定位点,决定着CALayer身上的哪个点会在position属性指定的位置
1》以自己的左上角为原点
2》它的x,y取值范围都是0~1,默认值为(0.5,0.5)意味着定位点在Layer的中间*/
@property CGPoint anchorPoint;
/* A transform applied to the layer relative to the anchor point of its * bounds rect. Defaults to the identity transform. Animatable. 形变属性 */
@property CATransform3D transform;
@property(nullable, strong) id contents;//内容
@property CGFloat borderWidth;//边框宽度
@property(nullable) CGColorRef borderColor;//边框颜色
@property(nullable) CGColorRef backgroundColor;//背景颜色
@property CGFloat cornerRadius;//圆角半径
/**
阴影属性
*/
@property(nullable) CGColorRef shadowColor;//阴影颜色
@property float shadowOpacity;//阴影不透明(0.0~1.0)
@property CGSize shadowOffset;//阴影偏移位置
4. x\y\z轴
5. 关于CALayer(可移植性)
QuartzCore
框架和CoreGraphics
框架是可以跨平台使用的,在iOS和Mac OS X上都能使用,但是UIKit只能在iOS中使用。所以:
为了保证可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
-
CALayer是定义在
QuartzCore
框架中的(Core Animation) -
CGImageRef、CGColorRef两种数据类型是定义在
CoreGraphics
框架中的 -
UIColor、UIImage是定义在
UIKit
框架中的
II UIView和CALayer的选择
由于CALayer
不能处理用户的触摸事件(@interface CALayer : NSObject <NSCoding, CAMediaTiming>
)
所以如果显示出来的东西需要跟用户进行交互的时候,选择UIView;其他情况UIView、CA Layer都可以选择;CALayer的性能会高些(更加轻量级些)
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment>
只要继承UIResponder( 响应者) 此类,才可以处理事件。