So I have been playing around with animations lately and I've come across the anchor point. I understand that the anchor point is (0.5, 0.5) by default, which is the middle of the view, and you can change it so that the anchor point is on one of the borders. My question is, how do I go on about this if I want my view to rotate around a specific point in the view's superview? Any help is greatly appreciated
Asked
Active
Viewed 220 times
2 Answers
0
Checkout this question how to rotate CALayer at one point
Also - although I guess you've probably already done this - have a read of Core Animation Programming Guide, Layer Geometry and Transforms.
Your question differs in that you want to specify a rotation point that is in your view's superview. To do that, you want to convert that superview point to the subview. You can do that as follows:
- Take your superview bounds, e.g. (0, 0, 500, 500)
- Take your subview frame, e.g. (50, 50, 100, 100)
- Take your superview rotation point, e.g. (75, 75)
Convert that to a point relative to the subview as follows:
CGFloat subviewX = 75.0f - subview.frame.x; CGFloat subviewX = 75.0f - subview.frame.y;
That gives the result expected (25.0f, 25.0f)
To rotate a CALayer with a CABasicAnimation, have a look at the selected answer for this question: CALayer with rotation animation.

Community
- 1
- 1

Max MacLeod
- 26,115
- 13
- 104
- 132
-
Thank you, but I'm doing this with CABasicAnimation, so what you gave me here doesn't apply to my case... Any idea how else to do this? Thanks! – kopproduction Oct 05 '11 at 22:11
-
Okay, I'm a little confused now.. The link really only tells me how to do a rotation with CABasicAnimation, which I already know. What I'm looking for is basically what you did above, calculating the rotation point in the superview, but what you did doesn't work for me because the coordinate system in the layer is different, if (0.5, 0.5) is the center, for example, 25 would be _way_ off screen...this is driving me crazy, any further suggestions you can give me? – kopproduction Oct 06 '11 at 19:15
0
I figured it out myself: I wanted the anchor point to be on the left screen border, so I did the following:
CGFloat subviewX = ((1/view.frame.size.width)*view.frame.origin.x) * (-1);
CGFloat subviewY = 0.5;

kopproduction
- 465
- 5
- 19
-
excellent. Was going to answer but been a bit tied up today with bugs! – Max MacLeod Oct 07 '11 at 17:17