# 彻底理解position与anchorPoint

### 引言

CALayer的position点是哪一点呢?
anchorPoint与position有什么关系？

@property CGPoint position
@property CGPoint anchorPoint

### anchorPoint、position、frame

 1 2  position.x = frame.origin.x + 0.5 * bounds.size.width； position.y = frame.origin.y + 0.5 * bounds.size.height； 

 1 2  position.x = frame.origin.x + anchorPoint.x * bounds.size.width； position.y = frame.origin.y + anchorPoint.y * bounds.size.height；

 1 2  frame.origin.x = position.x - anchorPoint.x * bounds.size.width； frame.origin.y = position.y - anchorPoint.y * bounds.size.height；

### 理解与运用

Layers have an implicit frame that is a function of the position, bounds, anchorPoint, and transform properties.

Apple doc中还有一句描述是这样的：

When you specify the frame of a layer, position is set relative to the anchor point. When you specify the position of the layer, bounds is set relative to the anchor point.

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

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

### 总结

1、position是layer中的anchorPoint在superLayer中的位置坐标。
2、互不影响原则：单独修改position与anchorPoint中任何一个属性都不影响另一个属性。
3、frame、position与anchorPoint有以下关系：

 1 2  frame.origin.x = position.x - anchorPoint.x * bounds.size.width； frame.origin.y = position.y - anchorPoint.y * bounds.size.height；