Alex Wayne Alex Wayne - 1 year ago 140
iOS Question

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 = + 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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download