CALayer层的属性(转)
一.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层的属性(转)的更多相关文章
- iOS开发UI篇—CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- iOS开发UI 篇—CAlayer层的属性
一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...
- CALayer之mask属性-遮罩
CALayer有一个属性叫做mask. 这个属性本身就是个CALayer类型,有和其他图层一样的绘制和布局属性. 它类似于一个子图层,相对于父图层(即拥有该属性的图层)布局,但是它却不是一个普通的子图 ...
- Swift - CALayer的contents属性动画
Swift - CALayer的contents属性动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // LiveImageVi ...
- CALayer的additive属性解析
CALayer的additive属性解析 效果: 源码:https://github.com/RylanJIN/ShareOfCoreAnimation // // CAPartAViewContro ...
- C#在数据层过滤属性中的主键
C#使用泛型+反射做为数据层时,一个很都头疼的问题,如何让C#属性在程序里识别出哪个属性是主键,在拼接SQL时,不能把主键拼接到SQL语句里. 这个需要自定义一个属性.新建一个类文件,命名为Prosp ...
- 蓝牙BLE: ATT协议层中属性(Attribute)
ATT(Attribute Protocol)属性层是GATT和GAP的基础,它定义了BLE协议栈上层的数据结构和组织方式. 属性(Attribute)概念是ATT层的核心,ATT层定义了属性的内容, ...
- calayer 的特殊属性整理
calayer: An object that manages image-based content and allows you to perform animations on that con ...
随机推荐
- ATL模板库中的OLEDB与ADO
上次将OLEDB的所有内容基本上都说完了,从之前的示例上来看OLEDB中有许多变量的定义,什么结果集对象.session对象.命令对象,还有各种缓冲等等,总体上来说直接使用OLEDB写程序很麻烦,用很 ...
- 【Android】4.0 神一样的仪式感:Android第一个项目HelloWorld——eclipse
进入Eclipse,在左侧栏右击: 新建 “Android Application Project”项目,如果new之后没有,选择“Project”中“Android”目录下的 “Android Ap ...
- angular2-模块
Angular模块 (NgModule) Angular 模块是带有 @NgModule 装饰器函数的类. @NgModule接收一个元数据对象,该对象告诉 Angular 如何编译和运行模块代码. ...
- String字符串操作
char chars[] ={'a','b','c'}; String s = new String(chars); int len = s.length();//字符串长度 System.out.p ...
- 16_AOP入门准备_Jdk动态代理模式
[工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...
- Multidex (方法数超过限制的处理)
报错 : Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 ...
- mvc页面间的传值
本文大致讲解mvc前后端的传值方式,包括control向view.view向control.以及action向action. 回顾 我们回顾下在ASP.NET WebForms中,页面之间最常用的传值 ...
- Java 创建 ARM 虚拟机磁盘类型选择的问题
问题描述 在Azure 门户创建 ARM 虚拟机时,我们直接可以选择虚拟机的磁盘类型,但是在 Azure Management Libraries for Java 的 API 中我们无法找到直接设置 ...
- Spring中<context:annotation-config/>的作用
spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...
- VSCode cpptools 插件在Centos 7下不能正确显示符号列表的解决办法
vscode 的插件cpptools 0.9.3 需要glibc 2.18的版本,但是Centos 7 下没有这个版本的GLIBC,所以导致链接库丢失,后台服务不能正常运行.按以下步骤操作可修复此问题 ...