Alex Wayne
Alex Wayne

Reputation: 186984

Why does a UIView rotation transform have to be reset before positioning?

I have few lines of code that put a north icon on the map. This icon moves around a bit, and must be angle toward north if the map is rotated.

But in order to make this work, I had to add a line of code that I don't understand.

northIcon.transform = CGAffineTransform.identity // why do I need this line?
northIcon.frame.origin.x = insets.left + 8
northIcon.frame.origin.y = insets.top  + 25
northIcon.transform = CGAffineTransform(rotationAngle: site.angle)

Without that first line, the first time this code is ran it works fine, but the next run through it get very tiny until it disappears, as if it resizing relative to the previous state in a way I don't get. I fixed this by forcing the transform to identity, then moving it, then rotating it back.

But why do I have to set the transform to identity? It was my understanding that the frame is calculated completely independently of the transform. And then after that the transform kicks in and change where/how the final view is drawn.

But clearly that's wrong, because if it were true I wouldn't need to reset the transform first.

Does anyone know what's going on here?

Upvotes: 0

Views: 900

Answers (1)

Duncan C
Duncan C

Reputation: 131398

A view's frame property is only defined if the transform is the identity transform. When it's not, the results of reading or manipulating the frame property are "undefined."

You can use the center property even when you've set the transform to a non-identity value though.

Upvotes: 2

Related Questions