每当修改一个CALayer的anchorPoint属性时,都要重新设置CALayer的position坐标
1.CALayer *layer = [myView layer];
1. CALayer *layer = [myView layer];
2. CGPoint oldAnchorPoint = layer.anchorPoint;
3. [layer setAnchorPoint:CGPointMake(1.0, 1.0)];
4. [layer setPosition:CGPointMake(layer.position.x + layer.bounds.size.width * (layer.anchorPoint.x - oldAnchorPoint.x), layer.position.y + layer.bounds.size.height * (layer.anchorPoint.y - oldAnchorPoint.y))];
为什么呢?
首先要清楚,UIView的frame属性是由center和bounds属性计算得到的。
frame.origin.x = center.x - bounds.size.width/2.0;
frame.origin.y = center.y - bounds.size.height/2.0;
frame.size = bounds.size;
相对的,身为UIView下级结构的CALayer呢?
CALayer的position(相当于center),bounds,anchorPoint是什么关系呢?
虽然没有frame,但是CALayer的显示(虚拟frame)也是由这些组件算出来的
frame.origin.x = position.x - anchorPoint.x * bounds.size.width/2.0;
frame.origin.y = position.y - anchorPoint.x * bounds.size.height/2.0;
frame.size = bounds.size;
所以,当我们在上面修改anchorPoint的时候,实际上修改了显示的运算元素!这样当anchorPoint修改为(1.0,1.0)的时候,经过重新运算,CALayer向左上角移动了
本文详细解析了CALayer中anchorPoint与position属性的关系及其影响。通过具体示例展示了如何在修改anchorPoint后更新position坐标,以确保视图正确显示。
2万+

被折叠的 条评论
为什么被折叠?



