Question about anchor point
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 appre开发者_Python百科ciated
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.
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;
精彩评论