一.position和anchorPoint 
1.简单介绍 
CALayer有2个非常重要的属性:position和anchorPoint 
  position: 
  (1)用来设置CALayer在父层中的位置 
  (2)以父层的左上角为原点(0,0) 
anchorPoint: 
  (1)称为”定位点”,”锚点” 
  (2)决定着CALayer身上的哪个点会在position属性所指的位置 
  (3)以自己的左上角为原点(0,0) 
  (4)它的x,y取值范围都是0~1,默认值为(0.5,0.5)

2.图示 
  anchorPoint 它的取值为0~1

红色图层的anchorPoint为(0,0)

红色图层的anchorPoint为(0.5,0.5)

红色图层的anchorPoint为(1,1)

红色图层的anchorPoint为(0.5,0)

position和anchorPoint 
添加一个红色图层到绿色图层上,红色图层显示到什么位置,由position属性决定 
假设红色图层的position是(100,100) 
到底把红色图层的哪个点移动到(100,100)的坐标位置,锚点。 
红色图层的锚点是(0,0)

红色图层的锚点是(0.5,0.5)

红色图层的锚点是(1,1)

红色图层的锚点是(0.5,0)

3.代码示例 
(1)没有设置锚点。默认的锚点位置为(0.5,0.5)

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//创建图层
CALayer *layer = [CALayer layer];
//设置图层的属性
layer.backgroundColor = [UIColor redColor].CGColor;
layer.bounds = CGRectMake(, , , );
//添加图层
[self.view.layer addSublayer:layer];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

(2)设置锚点为(0,0)

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//创建图层
CALayer *layer = [CALayer layer];
//设置图层的属性
layer.backgroundColor = [UIColor redColor].CGColor;
layer.bounds = CGRectMake(, , , );
//设置锚点为(0,0)
layer.anchorPoint = CGPointZero;
//添加图层
[self.view.layer addSublayer:layer];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

二.隐式动画 

1.简单说明 
  每一个UIView内部都默认关联着一个CALayer,我们称这个Layer为Root Layer(根层)。 所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动画 
  什么是隐式动画? 当对非Root Layer的部分属性进行修改时,默认会自动产生一些动画效果。 而这些属性称为Animatable Properties(可动画属性)。 

举例几个常见的可动画属性 
  (1)bounds:用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画 
  (2)backgroudColor:用于设置CALayer的背景色。修改这个属性会产生背景色的渐变动画。 
  (3)position:用于设置CALayer的位置。修改这个属性会产生平移动画。 
2.代码示例

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//创建图层
CALayer *layer = [CALayer layer];
//设置图层属性
layer.backgroundColor = [UIColor brownColor].CGColor;
layer.bounds = CGRectMake(, , , );
//显示位置
layer.position = CGPointMake(, );
layer.anchorPoint = CGPointZero;
layer.cornerRadius = ;
//添加图层
[self.view.layer addSublayer:layer];
_myLayer = layer;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//隐式动画
_myLayer.bounds = CGRectMake(, , , );
_myLayer.backgroundColor = [UIColor yellowColor].CGColor;
} @end

关闭隐式动画

 //关闭隐式动画
[CATransaction begin];
[CATransaction setDisableActions:YES];
//隐式动画
_myLayer.bounds = CGRectMake(, , , );
_myLayer.backgroundColor = [UIColor yellowColor].CGColor;
[CATransaction commit];

CALayer层的属性(转)的更多相关文章

  1. iOS开发UI篇—CAlayer层的属性

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  2. CAlayer层的属性

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  3. iOS开发UI 篇—CAlayer层的属性

    一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...

  4. CALayer之mask属性-遮罩

    CALayer有一个属性叫做mask. 这个属性本身就是个CALayer类型,有和其他图层一样的绘制和布局属性. 它类似于一个子图层,相对于父图层(即拥有该属性的图层)布局,但是它却不是一个普通的子图 ...

  5. Swift - CALayer的contents属性动画

    Swift - CALayer的contents属性动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // LiveImageVi ...

  6. CALayer的additive属性解析

    CALayer的additive属性解析 效果: 源码:https://github.com/RylanJIN/ShareOfCoreAnimation // // CAPartAViewContro ...

  7. C#在数据层过滤属性中的主键

    C#使用泛型+反射做为数据层时,一个很都头疼的问题,如何让C#属性在程序里识别出哪个属性是主键,在拼接SQL时,不能把主键拼接到SQL语句里. 这个需要自定义一个属性.新建一个类文件,命名为Prosp ...

  8. 蓝牙BLE: ATT协议层中属性(Attribute)

    ATT(Attribute Protocol)属性层是GATT和GAP的基础,它定义了BLE协议栈上层的数据结构和组织方式. 属性(Attribute)概念是ATT层的核心,ATT层定义了属性的内容, ...

  9. calayer 的特殊属性整理

    calayer: An object that manages image-based content and allows you to perform animations on that con ...

随机推荐

  1. Dozer 实现对象间属性复制

    使用场景:两个领域之间对象转换. 比如:在系统分层解耦过程中, 对外facade接口,一般使用VO对象,而内core业务逻辑层或者数据层通常使用Entity实体. VO对象 package com.m ...

  2. javascript: iframe switchSysBar 左欄打開關閉,兼容各瀏覽器操作

    <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Con ...

  3. webstorm 配置Vue.js 语法提示

    标签属性 v-text v-html v-once v-if v-show v-else v-for v-on v-bind v-model v-ref v-el v-pre v-cloak v-on ...

  4. 04_Spring中使用Quartz

    [Spring中使用SimplerTrigger] [QuartzTask.java] package com.higgin.task; import java.text.SimpleDateForm ...

  5. 基本类型int强转short时发生了什么?

    我们知道java中一个int类型占32bits(4字节),一个short占16bits(2字节)强制转换时只取低16位(short类型占的那16位),高16位(去掉低位多出来的那部分)属于溢出不计算, ...

  6. June 12th 2017 Week 24th Monday

    All the splendor in the world is not worth a good friend. 人世间所有的荣华富贵都比不上有一个好朋友. It's great to have a ...

  7. python创建项目

    一.准备下载 python3.6.6 https://www.python.org/downloads/windows/(需要注意你的电脑是32位还是64位) mysql 5.1.72 https:/ ...

  8. TOEFL考试(一年半的复仇,裸考)

    8/11/2018 had a TOEFL test without preparation. Reading (worry about too much, not familiar with the ...

  9. python接口测试-项目实践(七)脚本优化

    七 脚本优化:重复代码的提取成函数:与项目接口相关的都封装到一个类中:添加手工验证脚本,增加输入值的判断逻辑 将所有与该项目接口相关的封装成类 class ProjectApi: #3个数据源接口 d ...

  10. HDU 5527 贪心

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...