关于view的变换

来源:互联网 发布:经颅多普勒正常值数据 编辑:程序博客网 时间:2024/05/22 10:43

绕某点旋转:不得不知layer.anchor

从wzyfly的博客摘录一点:“UIView是iOS系统中界面元素的基础,所有的界面元素都是继承自它。它本身完全是由CoreAnimation来实现的。它真正的绘图部分,是由一个CALayer类来管理。UIView本身更像是一个CALayer的管理器,访问它的跟绘图和跟坐标有关的属性,例如frame,bounds等,实际上内部都是在访问它所包含的CALayer的相关属性。”

CAlayer有个重要的属性与旋转view有关,那就是anchor,锚。CALayer.h里定义是:
/* Defines the anchor point of the layer's bounds rect, as a point in
 * normalized layer coordinates - '(0, 0)' is the bottom left corner of
 * the bounds rect, '(1, 1)' is the top right corner. Defaults to
 * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */

anchorPoint服从Uikit坐标系,x轴水平向右,y轴垂直向下,也就是说,view的左上角anchor为(0,0),右下角为(1,1),默认为(0.5,0.5)在view的中心,若要设置某view以左上角为圆心旋转,需要设置

view.layer.anchorPoint = CGPointMake(0,0);
不过通过测试,我们知道上一句仅仅是更改了view的frame.origin的值,可以理解为,原来的anchorPoint在view的中间,现在anchorPoint指定点从中间改为view的左上角了不过是anchorPoint的位置没变,还在原来那个view的中间,因此,为了服从这点,view的frame就必须跟着变了。嗯,应该有人能理解我的意思吧。( anchorPoint改变前后都指向oldView的中心点的位置

因此,为了view不移位,我们可以重置下frame就行了,当然也可以更改view.layer.position的位置,不过那个要麻烦点,所以现在就暂时用frame吧。

CGRect oldRect = _sliderA.frame;//改写layer.anchorPoint_sliderA.frame = oldRect;

view的放大缩小:看看下面这个链接就好吧

iOS坑:UIView的frame和transfrom






0 0