ios开发讲解之anchorPoint和position详解

引言函数

        相信初接触到CALayer的人都会遇到如下几个问题:  为何修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与position有什么关系?测试

         每个UIView都默认关联着一个CALayer, UIView有frame、bounds和center三个属性,CALayer也有相似的属性,分别为frame、bounds、position、anchorPoint。那position、anchorPoint是什么呢?先看看二者的原型,可知都是CGPoint点。spa

    

 @property CGPoint position ;

 @property CGPoint anchorPoint ;

 

 

 

anchorPointcode

      通常都是先介绍position,再介绍anchorPoint。这里反过来,先来讲说anchorPoint。blog

       从一个例子开始入手吧,想象一下,把一张A4白纸用图钉订在书桌上,若是订得不是很紧的话,白纸就能够沿顺时针或逆时针方向围绕图钉旋转,这时候图钉就起着支点的做用。原型

咱们要解释的anchorPoint就至关于白纸上的图钉,它主要的做用就是用来做为变换的支点,旋转就是一种变换,相似的还有平移、缩放。it

     

 继续扩展,很明显,白纸的旋转形态随图钉的位置不一样而不一样,图钉订在白纸的正中间与左上角时分别造就了两种旋转形态,这是由图钉(anchorPoint)的位置决定的。io

如何衡量图钉(anchorPoint)在白纸中的位置呢?class

在iOS中,anchorPoint点的值是用一种相对bounds的比例值来肯定的,在白纸的左上角、右下角,anchorPoint分为为(0,0), (1, 1),也就是说anchorPoint是在单元坐标空间(同时也是左手坐标系)中定义的。扩展

相似地,能够得出在白纸的中心点、左下角和右上角的anchorPoint为(0.5,0.5), (0,1), (1,0)。

 

而后再来看下面图:

 

 

position

       确切地说,position是layer中的anchorPoint点在superLayer中的位置坐标。所以能够说, position点是相对suerLayer的,anchorPoint点是相对layer的,二者是相对不一样的坐标空间的一个重合点。

      再来看看position的原始定义: The layer’s position in its superlayer’s coordinate space。 中文能够理解成为position是layer相对superLayer坐标空间的位置,很显然,这里的位置是根据anchorPoint来肯定的.

 

 

anchorPoint、position、frame

      anchorPoint的默认值为(0.5,0.5),也就是anchorPoint默认在layer的中心点。默认状况下,使用addSublayer函数添加layer时,若是已知layer的frame值,根据上面的结论,那么position的值即可以用下面的公式计算:

position.x = frame.origin.x + 0.5 * bounds.size.width; position.y = frame.origin.y + 0.5 * bounds.size.height;

里面的0.5是由于anchorPoint取默认值,更通用的公式应该是下面的:

position.x = frame.origin.x + anchorPoint.x * bounds.size.width; position.y = frame.origin.y + anchorPoint.y * bounds.size.height;

 

特别注意

下面再来看另外两个问题,若是单方面修改layer的position位置,会对anchorPoint有什么影响呢?修改anchorPoint又如何影响position呢?  

根据代码测试,二者互不影响,受影响的只会是frame.origin,也就是layer坐标原点相对superLayer会有所改变。换句话说,frame.origin由position和anchorPoint共同决定,上面的公式能够变换成下面这样的:

frame.origin.x = position.x - anchorPoint.x * bounds.size.width; frame.origin.y = position.y - anchorPoint.y * bounds.size.height;

这就解释了为何修改anchorPoint会移动layer,由于position不受影响,只能是frame.origin作相应的改变,于是会移动layer。

 

理解与运用

        由于 position点和anchorPoint点是独立的,本身不会由于另一个的改变而发生变化,可是在实际状况中,每每有这样一种需求,我须要修改anchorPoint,但又不想要移动layer也就是不想修改frame.origin,那么根据前面的公式,就须要position作相应地修改。简单地推导,能够获得下面的公式:

positionNew.x = positionOld.x + (anchorPointNew.x - anchorPointOld.x) * bounds.size.width positionNew.y = positionOld.y + (anchorPointNew.y - anchorPointOld.y) * bounds.size.height

      可是在实际使用不必这么麻烦。修改anchorPoint而不想移动layer,在修改anchorPoint后再从新设置一遍frame就能够达到目的,这时position就会自动进行相应的改变。写成函数就是下面这样的:

- (void) setAnchorPoint:(CGPoint)anchorpoint forView:(UIView *)view{ CGRect oldFrame = view; view.layer.anchorPoint = anchorpoint; view.frame = oldFrame; }

 

总结

   一、position是layer中的anchorPoint在superLayer中的位置坐标.

   二、互不影响原则:单独修改position与anchorPoint中任何一个属性都不影响另外一个属性。   

   三、frame、position与anchorPoint有如下关系:

frame.origin.x = position.x - anchorPoint.x * bounds.size.width; frame.origin.y = position.y - anchorPoint.y * bounds.size.height;

相关文章
相关标签/搜索