0
votes

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).

1
I can't reproduce this. Are you sure you don't have other transforms applied to the button? You can't arbitrarily rotate something that has been translated for instance. Please provide a MCVE stackoverflow.com/help/mcve.Rob Napier
@RobNapier: aha, it occurs if I set my button's position in viewDidLayoutSubviews. If I don't attempt to position it there, the rotation works normally as expected.MusiGenesis

1 Answers

0
votes

So my problem was that the transforms get hosed when you reposition a view in viewDidLayoutSubviews (or if it's repositioned in any way, like via the autoresizingMask property).

I'm sure there's a way to solve this with actual knowledge of how transforms work, but my simple workaround is to just place my button into another view the same size as the button, and then just reposition this container view where I need it to be.