I have a UIButton and I want it to rotate by a set amount whenever I tap it. If I use this code and rotate it exactly 90 degrees counter-clockwise:
CGAffineTransform transform = CGAffineTransformRotate(self.transform,
(-M_PI * 2.0) * 0.25);
self.transform = transform;
everything works as expected (this also works if I use a positive value and rotate clockwise). If, however, I try to rotate the button by 45 degrees:
CGAffineTransform transform = CGAffineTransformRotate(self.transform,
(-M_PI * 2.0) * 0.125);
my button just vanishes entirely. If I do:
CGAffineTransform transform = CGAffineTransformRotate(self.transform,
(-M_PI * 2.0) * 0.20);
the button is weirdly elongated although the angle appears to be correct.
I just want to rotate my button in place, without any resizing, at whatever arbitrary angle I want - what am I doing wrong here?
Update: this only occurs if I attempt to move/position my button inside of viewDidLayoutSubviews
. I'm guessing this is because the change of the transform results in a call to viewDidLayoutSubviews, at which point the positioning is all screwed up.
If I don't attempt to position the button in viewDidLayoutSubviews, and only set the autoresizingMask when the button is loaded, it keeps its proper position in the middle - unless I rotate it by changing the transform. This rotation works and rotates the button in place, but as soon as I rotate the device to landscape and back to portrait (sometimes just when I go to landscape) the button vanishes entirely.
This trick only seems to work for a control that never has to move (other than the spinning thing).
viewDidLayoutSubviews
. If I don't attempt to position it there, the rotation works normally as expected. – MusiGenesis