彻底理解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 属性的更多相关文章

  1. iOS学习--详解UIView的 contentStretch属性

    通过实例和图片理解UIView的contentStretch属性 方法 通过一个图片建立一个简单的UIImageView 设置它的contentStretch属性 修改它的frame属性 观察 测试用 ...

  2. CALayer 的 position和anchorPoint属性

    在iOS 中,UIButton.UIImage等UIView 之所以能够显示在屏幕上,是因为其内部有一个图层(CALayer).通过UIView的layer 属性可以访问这个图层: @property ...

  3. Position & anchorPoint 深入

    引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...

  4. UIView的常见属性

    UIView的常见属性: @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDyn ...

  5. UIView的transform属性

    一.什么是Transform Transform(变化矩阵)是一种3×3的矩阵,如下图所示: 通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作.而且矩阵的操作不具备交换律 ...

  6. iOS--------坐标系统(UIView的frame、bounds跟center属性)

    1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...

  7. 关于UIView的userInteractionEnabled属性

    关于UIView的userInteractionEnabled属性 如果父视图为ParentView包含一个Button,如果再ParentView上添加子视图ChildView,且ChildView ...

  8. UIView的clipsToBounds属性,layoutSubViews及触摸事件传递(默认情况下)总结

    一.UIView的clipsToBounds属性 * 默认情况下,超出父控件尺寸范围的子控件还是可见的 * 如果设置父控件的clipsToBounds=YES,就会裁剪掉超出父控件尺寸范围内的子控件, ...

  9. UIView之常用属性

    UIView之常用属性 1. view.tag = 200; // 系统保留0-1002. view.frame = CGRectMake(20, 30, 300, 300);3. view.cent ...

随机推荐

  1. SQL Injection绕过技巧

    0x00 sql注入的原因 sql注入的原因,表面上说是因为 拼接字符串,构成sql语句,没有使用 sql语句预编译,绑定变量. 但是更深层次的原因是,将用户输入的字符串,当成了 "sql语 ...

  2. war 宽度变窄

    1.打开开始菜单-运行-输入Regedit 打开注册表编辑器 展开 HKEY_CURRENT_USER 继续展开 Software继续展开 Blizzard Entertainment 在Warcra ...

  3. 关于pthread_cond_wait使用while循环判断的理解

    在Stevens的<Unix 环境高级编程>中第11章线程关于pthread_cond_wait的介绍中有一个生产者-消费者的例子P311,在进入pthread_cond_wait前使用w ...

  4. Zookeeper客户端使用

    参考链接: http://blog.csdn.net/jason5186/article/details/46314381 http://ifeve.com/zookeeper-path-cache/

  5. html静态页面调用php文件

    如在页面a.html中用下面这句调用,可以将action=test的参数传递到b.php. Javascript代码 <script type="text/javascript&quo ...

  6. Java类对象数组声明和初始化

    Java是纯面向对象语言.类是其重要构成单位. 然后,在实际编程中,我们会自己定义一些类,如Point <span style="font-size:14px;">pu ...

  7. iOS边练边学--UIPickerView和UIDatePicker的简单使用

    一.点菜系统练习(UIPickerView) <1>UIPickerView的常用代理方法介绍 #pragma mark - <UIPickerViewDelegate> // ...

  8. Eclipse 中link一个异地的Folder

    Eclipse 中link一个外地的Folder New -> Folder -> Click "Advanced" --> Check "Link t ...

  9. Qt信号槽的一些事

    注:此文是站在Qt5的角度说的,对于Qt4部分是不适用的. 1.先说Qt信号槽的几种连接方式和执行方式. 1)Qt信号槽给出了五种连接方式: Qt::AutoConnection 0 自动连接:默认的 ...

  10. 【Java面试题】10 abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?

    1.abstract是抽象的,指的是方法只有声明而没有实现,他的实现要放入声明该类的子类中实现. 2.static是静态的,是一种属于类而不属于对象的方法或者属性 3.synchronized 是同步 ...