Warpling
Warpling

Reputation: 2105

Why doesn't the value a CALayer's "transform.scale" match its transform scale?

Imagine you're trying to scale a layer and then later read that scale back out (for example: during an animation that's changing the scale).

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"

The scale value read out using value(forKeyPath:) will be close but not quite right!

Upvotes: 0

Views: 411

Answers (1)

Warpling
Warpling

Reputation: 2105

This is probably because the value read out has something to do with the whole 3D scale (think in x, y, and z). Reading out the x or y scale specifically will return the right value:

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"

Upvotes: 1

Related Questions