关于锚点的问题

发布于 2024-12-06 12:29:04 字数 122 浏览 0 评论 0原文

所以我最近一直在玩动画并且遇到了锚点。据我了解,锚点默认为 (0.5, 0.5),即视图的中间,您可以更改它,使锚点位于其中一个边框上。我的问题是,如果我希望我的视图围绕视图的超级视图中的特定点旋转,我该如何继续?非常感谢任何帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-12-13 12:29:04

查看这个问题如何在某一点旋转 CALayer

另外 - 尽管我我猜你可能已经这样做了 - 阅读《核心动画编程指南》,图层几何和变换

您的问题的不同之处在于您想要指定视图的超级视图中的旋转点。为此,您需要将该超级视图点转换为子视图。您可以按如下方式执行此操作:

  1. 获取您的超级视图边界,例如 (0, 0, 500, 500)
  2. 获取您的子视图框架,例如 (50, 50, 100, 100)
  3. 获取您的超级视图旋转点,例如 (75, 75)
  4. < p>将其转换为相对于子视图的点,如下所示:

    CGFloat subviewX = 75.0f - subview.frame.x;
    CGFloat subviewX = 75.0f - subview.frame.y;
    
  5. 给出预期结果 (25.0f, 25.0f)

要使用 CABasicAnimation 旋转 CALayer,请查看此问题的选定答案:具有旋转动画的 CALayer

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.

愁以何悠 2024-12-13 12:29:04

我自己弄清楚了:我希望锚点位于屏幕左侧边框上,所以我执行了以下操作:

CGFloat subviewX = ((1/view.frame.size.width)*view.frame.origin.x) * (-1);
CGFloat subviewY = 0.5;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文