CALayer的position,anchorPoint属性 与UIView的frame 属性
彻底理解CALayer的position,anchorPoint属性 与UIView的frame 属性
一、position,anchorPoint两者都是CALayer的属性,都是CGPoint点
CALayer有2个非常重要的属性:position和anchorPoint
@property CGPoint position;
用来设置CALayer在父层中的位置
以父层的左上角为原点(0, 0)
@property CGPoint anchorPoint;
称为“定位点”、“锚点”
决定着CALayer身上的哪个点会在position属性所指的位置
以自己的左上角为原点(0, 0)
它的x、y取值范围都是0~1,默认值为(0.5, 0.5)
从一个例子开始入手吧,想象一下,把一张A4白纸用图钉订在书桌上,如果订得不是很紧的话,白纸就可以沿顺时针或逆时针方向围绕图钉旋转,这时候图钉就起着支点的作用。我们要解释的anchorPoint就相当于白纸上的图钉,它主要的作用就是用来作为变换的支点,旋转就是一种变换,类似的还有平移、缩放!而position是钉子在墙上的位置!像UIView有superView与subView的概念一样,CALayer也有superLayer与layer的概念
frame.origin由position和anchorPoint共同决定
frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
frame.origin.y = position.y - anchorPoint.y * bounds.size.height;
例如 frame为(10,10,100,100)
10 = y-0.5*100
10 = y-0.5*100
这时候layer的posion为(60,60) anchorPoint为(0,5,0,5);
锚点的相关应用
下面结合UIView动画来解析 anchorPoint 对动画的影响
UIView的隐藏:(这里添加了弹簧效果)
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity: options:UIViewAnimationOptionCurveEaseIn animations:^{
self.animationView.transform = CGAffineTransformMakeScale(, 0.0001);
} completion:nil];
UIView的显示:
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity: options:UIViewAnimationOptionCurveEaseIn animations:^{
self.animationView.transform = CGAffineTransformIdentity;
} completion:nil];
1、CALayer的anchorPoint的默认位置为(0.5,0.5),即其bounds的center
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(0.5,0.5);
2、设置anchorPoint的默认位置为(0,0)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(,);
3、设置anchorPoint的默认位置为(0.5,0)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(0.5,);
4、设置anchorPoint的默认位置为(1.0,1.0)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(1.0,1.0);
5、设置anchorPoint的默认位置为(0,0.5)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(,0.5);
6、设置anchorPoint的默认位置为(1,0.5)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(,0.5);
7、设置anchorPoint的默认位置为(0,1.0)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(,1.0);
8、设置anchorPoint的默认位置为(1.0,1.0)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(1.0,1.0);
9、设置anchorPoint的X值为0~1,Y值为0,再设置transform的X轴的缩放比例为0.0001(稍微比0大一点,不然动画效果无效) (CGAffineTransformMakeScale(1, 0.0001)
// 设置锚点
animationView.layer.anchorPoint = CGPointMake(,);
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity: options:UIViewAnimationOptionCurveEaseIn animations:^{
self.animationView.transform = CGAffineTransformMakeScale(, 0.0001);
} completion:nil];
效果如下:
CALayer的position,anchorPoint属性 与UIView的frame 属性的更多相关文章
- iOS学习--详解UIView的 contentStretch属性
通过实例和图片理解UIView的contentStretch属性 方法 通过一个图片建立一个简单的UIImageView 设置它的contentStretch属性 修改它的frame属性 观察 测试用 ...
- CALayer 的 position和anchorPoint属性
在iOS 中,UIButton.UIImage等UIView 之所以能够显示在屏幕上,是因为其内部有一个图层(CALayer).通过UIView的layer 属性可以访问这个图层: @property ...
- Position & anchorPoint 深入
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...
- UIView的常见属性
UIView的常见属性: @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDyn ...
- UIView的transform属性
一.什么是Transform Transform(变化矩阵)是一种3×3的矩阵,如下图所示: 通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作.而且矩阵的操作不具备交换律 ...
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...
- 关于UIView的userInteractionEnabled属性
关于UIView的userInteractionEnabled属性 如果父视图为ParentView包含一个Button,如果再ParentView上添加子视图ChildView,且ChildView ...
- UIView的clipsToBounds属性,layoutSubViews及触摸事件传递(默认情况下)总结
一.UIView的clipsToBounds属性 * 默认情况下,超出父控件尺寸范围的子控件还是可见的 * 如果设置父控件的clipsToBounds=YES,就会裁剪掉超出父控件尺寸范围内的子控件, ...
- UIView之常用属性
UIView之常用属性 1. view.tag = 200; // 系统保留0-1002. view.frame = CGRectMake(20, 30, 300, 300);3. view.cent ...
随机推荐
- Android 自定义Adapter实现多视图Item的ListView
自定义Adapter实现多视图Item的ListView http://www.devdiv.com/adapter_item_listview-blog-20-7539.html 1.原理分析 Ad ...
- IE6、IE7、IE8、Firefox兼容性
整理关于IE6.IE7.IE8.Firefox兼容性CSS HACK问题 1.区别IE和非IE浏览器CSS HACK代码 #divcss5{background:blue; /*非IE 背景藍色*/b ...
- lvm 新建一个vg 逻辑卷 配置启动
fdisk /dev/sdb 格式 t 8e w vgcreate datavg /dev/sdb1lvcreate -L 999G -n lvdata datavgmkfs.xfs /dev/da ...
- iOS边练边学--xib文件初使用
一.Xib和storyboard对比 *共同点: 1>都用来描述软件界面 2>都用Interface Builder工具来编辑 3>本质都是转换成代码去创建控件 *不同点 1> ...
- BuddyPress创建组、查看成员信息等找不到页面
BuddyPress创建组.查看成员信息等找不到页面 http://aoxuangame.com/wordpress/groups/create/ http://aoxuangame.com/word ...
- 使用explain分析sql语句
sql语句优化 : sql语句的时间花在哪儿? 答: 等待时间 , 执行时间. 这两个时间并非孤立的, 如果单条语句执行的快了,对其他语句的锁定的也就少了. 所以,我们来分析如何降低执行时间. : s ...
- 【R】用 ggplot2 绘制漂亮的分级统计地图
最近我一直尝试利用R绘制地图,我从网上找到了上百种不同的实现方法,然而其中却没有适用于我的数据的方法.最终,我从以下几个博客[1]中找到了灵感.我在整合这些资源的基础上,通过不断的试验和修正得到了一个 ...
- 一段有用的javascript加密解密
今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码: <SCRIPT LANGUAGE="JavaScript" ...
- firefox插件之 vimperator 的使用
简介: vimperator 是 Firefox浏览器下的一个插件,可以让我们像使用vim 一样使用 firefox浏览器,高效畅快,不用鼠标了.它的官网为:http://www.vimperator ...
- python 向qq邮箱发邮件
#coding:utf-8 ''' Created on 2017-1-12 @author: xiaochun ''' import smtplib from email.mime.text imp ...