One area of writing code that I’ve never been good at is graphics. I partially attribute this to my lack of artistic ability, but also my distaste for advanced math. For the last day, I’ve been working on a problem to rotate some buttons and move the buttons at the same time. Rotating the buttons is easy:
self.myButton.transform = CGAffineTransformRotate(flashTransform, degrees * M_PI/180);
Moving the button turned out to be hard for me. I tried all kinds of translations and transforms, but came up with nothing. The transforms are matrix math and that just makes me want to turn the other way and run. I searched and searched, tried different things, stayed up late and still couldn’t solve it. This morning I was chatting with a colleague and he suggested just to do the rotation which I knew how to do and then move the buttons. OK, that seemed simple and I know I had tried it. Turns out you can’t move a view using the frame property if there is a non-identity transform on it; however, you can set the center which is just as good, but requires a small extra step. You have to convert the origin point you want to the center.
CGPoint center; center.x = newOrigin.x + self.button.bounds.size.width / 2; center.y = newOrigin.y + self.button.bounds.size.height / 2;
Definitely not hard to do, but it tripped me up. The other thing that tripped me up is that in order to resize the button, you have to use the bounds and not the frame if a transform is applied. So with those 2 hints, I was able to finish my task in about 15 minutes. Hopefully next time I won’t be so thick header about this.