彻底理解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. JS自动爆炸案例

    学习到了: setTimeout函数的灵活运用. 案例实现讲解: 1.先定义一个全局的变量,赋值为null. 2.然后使用timeout调用bang函数,以达到自动自动调用函数的功能. 3.bang函 ...

  2. js 函数参数 arguments[0]

    function box() {            return arguments[0] + '|' + arguments[1];                    }        al ...

  3. MySQL 源代码scr.rpm安装的一点注意事项

    rpm安装包通常为二进制包(Binary)以及源代码包(Source)两种形式. 在使用源代码方式安装MySQL的时候,官方站点上下载的源代码包通常为scr.rpm格式,而不是直接的tar包.对此,须 ...

  4. DALFactory有什么作用

    DAL是指Data Access Layer.DALFactory是用于创建数据訪问对象的工厂.本质上是採用了抽象工厂的设计模式.目的是支持多种数据訪问层,比方sql server和oracle两种实 ...

  5. 400错误,Required String parameter 'paramter' is not present

    1.就拿简单的登录来说吧,这是开始的代码 @RequestMapping(value="/login")public ModelAndView login(@RequestPara ...

  6. C语言写的trim()函数

    C语言的标准库中缺少对字符串进行操作的trim()函数,使用起来有些不便,可以使用利用 strlen 和 isspace 函数以及指针来自己写一个. 1.strlen 函数 原型:extern int ...

  7. ubuntu之Matlab安装

    (清华大学校内适用) 1.首先下载Matlab镜像:http://its.tsinghua.edu.cn/column/jsrj/1,一共有两个ios文件. 2.然后执行 cd ~ mkdir mat ...

  8. e660. 用一组像素创建图像

    This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...

  9. API Design Principles -- QT Project

    [the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...

  10. Python中tab键自动补全功能的配置

    新手学习Python的时候,如何没有tab键补全功能,我感觉那将是一个噩梦,对于我们这种菜鸟来说,刚接触python,对一切都不了解,还好有前辈们的指导,学习一下,并记录下来,还没有学习这个功能小伙伴 ...