修改 composer 枢轴调整(pivot adjustment)的基点,核心方法是直接操作 `pivotpoint` 属性。在 swift 中,该属性接受一个 `cgpoint` 值,其中 `(0, 0)` 代表左上角,`(1, 1)` 代表右下角,默认值为 `(0.5, 0.5)` 即中心点。通过设置具体的 x 和 y 坐标,你可以将旋转、缩放或变换的参考原点移动到视图的任何位置。

pivotPoint 的坐标系是如何定义的?
它基于归一化坐标系,范围从 0.0 到 1.0。X 轴从左向右增加,Y 轴从上向下增加。这意味着你不需要知道视图的具体像素尺寸,只需根据相对比例即可定位基点。

import UIKit
class PivotViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
view.backgroundColor = .blue
view.layer.borderWidth = 2
view.layer.borderColor = UIColor.white.cgColor
self.view.addSubview(view)
// 将基点设置在左下角
view.layer.pivotPoint = CGPoint(x: 0.0, y: 1.0)
// 执行旋转以验证基点变化
let rotation = CABasicAnimation(keyPath: "transform.rotation")
rotation.fromValue = 0
rotation.toValue = Double.pi * 2
rotation.duration = 2.0
rotation.isRemovedOnCompletion = false
rotation.fillMode = .forwards
view.layer.add(rotation, forKey: "spin")
}
}
运行上述代码,蓝色视图将围绕其左下角进行旋转。如果将 pivotPoint 改为 (1.0, 0.0),则基点变为右上角。这种归一化方式使得基点调整与视图大小解耦,无论视图如何缩放,基点始终保持在指定的相对位置。
基点调整对 anchorPoint 有什么影响?
修改 pivotPoint 会同步更新 CALayer 的 anchorPoint。在 Core Animation 中,anchorPoint 是一个归一化的点,表示图层自身坐标系中的锚点位置。当你设置 pivotPoint 时,系统会自动计算并应用相应的 anchorPoint 值,从而改变图层的几何变换基准。
专业音乐创作规划。基于 mmx-cli(MiniMax AI)生成高质量音乐和歌曲,自动规划曲风、节奏、乐器编配和人声特色,将模糊需求转化为精准 Prompt。支持纯器乐和中文歌词歌曲。

import UIKit
class AnchorPointDemo: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupLayer()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupLayer() {
backgroundColor = .clear
let subView = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
subView.backgroundColor = .red
addSubview(subView)
// 观察 anchorPoint 的变化
print("Default anchor point: \(subView.layer.anchorPoint)")
// 设置 pivotPoint
subView.layer.pivotPoint = CGPoint(x: 0.5, y: 0.5)
print("After setting pivot (0.5, 0.5): \(subView.layer.anchorPoint)")
subView.layer.pivotPoint = CGPoint(x: 0.0, y: 0.0)
print("After setting pivot (0.0, 0.0): \(subView.layer.anchorPoint)")
}
}
控制台输出显示,当 pivotPoint 为 (0.5, 0.5) 时,anchorPoint 也是 (0.5, 0.5);当 pivotPoint 设为 (0.0, 0.0) 时,anchorPoint 随之变为 (0.0, 0.0)。这表明两者是绑定的关系,修改其中一个即修改另一个。需要注意的是,anchorPoint 的改变会影响图层的位置,因为图层的位置是相对于其锚点在父图层坐标系中的位置。因此,在调整基点后,可能需要微调 frame 或 position 以保持视觉上的预期布局。
基点调整在动画过程中是否保持动态跟随?
是的,pivotPoint 是一个静态属性,一旦设置,它在动画过程中不会自动随视图内容或父视图变换而动态变化。它始终固定在视图自身的局部坐标系中。如果在动画期间需要改变基点,必须显式地再次设置 pivotPoint 属性。此外,由于 pivotPoint 影响的是图层的几何结构,它不会参与隐式动画,除非你显式地将其包含在动画块中。
import UIKit
class DynamicPivotViewController: UIViewController {
var redView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
redView.backgroundColor = .red
self.view.addSubview(redView)
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 300, width: 100, height: 40)
button.setTitle("Toggle Pivot", for: .normal)
button.addTarget(self, action: #selector(togglePivot), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func togglePivot() {
if redView.layer.anchorPoint == CGPoint(x: 0.5, y: 0.5) {
UIView.animate(withDuration: 0.5) {
self.redView.layer.pivotPoint = CGPoint(x: 0.0, y: 0.0)
self.redView.transform = CGAffineTransform(rotationAngle: .pi / 4)
}
} else {
UIView.animate(withDuration: 0.5) {
self.redView.layer.pivotPoint = CGPoint(x: 0.5, y: 0.5)
self.redView.transform = .identity
}
}
}
}
点击按钮后,视图不仅会旋转,其基点也会从中心切换到左上角。由于 UIView.animate 块内的属性更改会被视为动画的一部分,基点的切换会平滑过渡。然而,这种切换可能会导致视图在屏幕上的位置发生突变,因为锚点的改变会重新定义视图的原点。在实际开发中,如果需要保持视图位置不变,应在切换基点前后手动调整 position 属性以补偿偏移量。

















