0

レイヤーのアンカーポイント、位置、フレームの関係を確認するデモアプリを作成しました。

ビューの最初の外観は次のようになります。

初期設定

コードでは、赤いビューのアンカー ポイントを変更すると、次のようになります。アンカー ポイントの変更はそのビューのフレームに影響するため、理解できました。

ビューのアンカーポイントを (0,0) に変更します

ビューのフレームを元のフレームとして維持するために、次のコードを使用しました。

元のフレームを維持するコード

コンソールの出力から、フレームがすでに同じままであることがわかりました。

ただし、ビューの最終的な外観は次のようになりますが、それでも位置が変わります。

すべてのコードは次のようになります。

コード

最終結果

コードは次のとおりです。

// 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.
4

1 に答える 1