レイヤーのアンカーポイント、位置、フレームの関係を確認するデモアプリを作成しました。
ビューの最初の外観は次のようになります。
コードでは、赤いビューのアンカー ポイントを変更すると、次のようになります。アンカー ポイントの変更はそのビューのフレームに影響するため、理解できました。
ビューのフレームを元のフレームとして維持するために、次のコードを使用しました。
コンソールの出力から、フレームがすでに同じままであることがわかりました。
ただし、ビューの最終的な外観は次のようになりますが、それでも位置が変わります。
すべてのコードは次のようになります。
コードは次のとおりです。
// Reserve original frame
let oldFrame = self.controlledView.frame
// Here I changed the anchorPoint which will cause that view's frame change
self.controlledView.layer.anchorPoint = CGPoint(x: 0, y: 0)
// to avoid the change of frame, set it back
self.controlledView.frame = oldFrame
// From the console's log the frame doesn't change, the red view's final
// location should be the same with the first image. However it is aligned to the right,
// which I could not understand.