想象一下,您正在尝试缩放图层,然后稍后再读回该缩放比例(例如:在更改缩放比例的动画中)。
let desiredScale = 0.5
layer.transform = CATransform3DMakeScale(desiredScale, desiredScale, 1)
let readScale = layer.value(forKeyPath: "transform.scale") as? Double
print("scale: \(readScale)") // "scale: 0.6666666"
使用value(forKeyPath:)
读取的比例值将接近,但不太正确!
答案 0 :(得分:1)
这可能是因为读出的值与整个3D比例有关(考虑x,y和z)。专门读出x或y比例尺将返回正确的值:
let desiredScale = 0.5
layer.transform = CATransform3DMakeScale(desiredScale, desiredScale, 1)
let wholeScale = layer.value(forKeyPath: "transform.scale") as? Double
let xScale = layer.value(forKeyPath: "transform.scale.x") as? Double
print("scale: \(wholeScale)") // "scale: 0.6666666"
print("scale: \(xScale)") // "scale: 0.5"