开发者

Question about anchor point

开发者 https://www.devze.com 2023-04-08 14:25 出处:网络
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 c

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:

  1. Take your superview bounds, e.g. (0, 0, 500, 500)
  2. Take your subview frame, e.g. (50, 50, 100, 100)
  3. Take your superview rotation point, e.g. (75, 75)
  4. 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;
    
  5. 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号