很难找到旋转变换的正确锚点

发布于 2024-12-14 22:45:42 字数 1221 浏览 5 评论 0原文

我正在为按钮的位置设置动画,将其绕一圈旋转。这会创建类似 UISwitch 的行为,只不过它更加奇特。按钮会旋转,但最终会偏离所需位置约 0.25 弧度。我试图找出将锚点放置在哪里,以使按钮绕其原点完美旋转。

下面是我用来使按钮以 120 像素半径从原始位置“旋转”的代码。

float offsetX=120;
float offsetY=0;

enableDisableButton.layer.anchorPoint =
     CGPointMake(offsetX/enableDisableButton.frame.size.width,                 
                 offsetY/enableDisableButton.frame.size.height);

我使用以下方法进行计算。传递 90 度的参数,我希望看到按钮从 180° 位置开始移动到 90°,距离原点仍然有 120 像素。

-(CGAffineTransform)calculateLabelPositionFromFixedPointWithOffsetClockwiseInDegrees:(float)degrees
{

    float rotation = degrees*(2*M_PI/360);
    CGAffineTransform transform24 = CGAffineTransformMakeRotation(rotation);
    NSLog(@"rotation: %f", rotation);
    return transform24;
}

这个 0.25 弧度左右的偏移意味着我需要直观地确认每个按钮的位置,并且无法轻松地以编程方式调整其位置。任何帮助确定旋转按钮的正确方法的帮助都是值得赞赏的。

我尝试了其他方法来旋转对象。例如,我有一个箭头 <--------x,我想围绕 x 旋转这个箭头。此操作的正确锚点位置是什么?是[1, 0.5]吗?

进行这种旋转的一种更简单的方法是将对象放入对称的 UIView 中,将其置于所需的旋转点中心并分配旋转变换。这可行,但要求视图是原来的两倍:

Y----x----Y Y----x----Y Y----x----Y Y----x----Y这会围绕中心点 X 旋转 Y,而无需任何锚点调整。这是在模拟仪表等内旋转箭头的非常有用的方法。

I'm animating a button's position, rotating it around a circle. This creates a UISwitch-like behavior, except it is a lot more fancy. The button rotates, but it ends up off the desired position by about 0.25 radians. I'm trying to figure out where to put the anchor point to make the button rotate in a perfect circle around its origin.

Here's the code that I use to make the button "orbit" with a 120 pixel radius from the original location.

float offsetX=120;
float offsetY=0;

enableDisableButton.layer.anchorPoint =
     CGPointMake(offsetX/enableDisableButton.frame.size.width,                 
                 offsetY/enableDisableButton.frame.size.height);

I use the following method to do the calculations. Passing an argument of 90 for degrees, I expect to see the button start at a 180˚ position and move to 90˚, still 120 pixels away from its origin

-(CGAffineTransform)calculateLabelPositionFromFixedPointWithOffsetClockwiseInDegrees:(float)degrees
{

    float rotation = degrees*(2*M_PI/360);
    CGAffineTransform transform24 = CGAffineTransformMakeRotation(rotation);
    NSLog(@"rotation: %f", rotation);
    return transform24;
}

This little 0.25 radian or so offset means that I need to visually confirm the location of each button and cannot easily adjust its location programmatically. Any help in determining the correct way to rotate the button is appreciated.

I tried other ways to rotate objects. For example, I have an arrow
<--------x, and I would like to rotate this arrow in a circle around x . What is the correct anchor point placement for this operation? Is it [1, 0.5]?

An easier way to do this kind of rotations is to put an object within a symmetric UIView, center it at the desired point of rotation and assign a rotation transform. This works, but requires the view to be twice as big:

Y----x----Y < this rotates Y around center point X without any anchor point adjustments. this is a very useful method to rotate arrows within analog gauges and such.

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

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

发布评论

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

评论(2

无可置疑 2024-12-21 22:45:43

示例中 enableDisableButton 的锚点将

CGPointMake(offsetX/enableDisableButton.frame.size.width, 0)

不在按钮中垂直居中。围绕锚点旋转将导致按钮偏移到所需位置的顶部或底部。变换看起来不错,所以我认为它只是锚点。应该是:

CGPointMake(offsetX/enableDisableButton.frame.size.width, 0.5f)

The anchor point of your enableDisableButton in the example would be

CGPointMake(offsetX/enableDisableButton.frame.size.width, 0)

i.e. not vertically centered in your button. Rotating around the anchor point would result in the button being offset to the top or bottom of the desired position. The transform looks alright, so I think it is just the anchor point. It should be:

CGPointMake(offsetX/enableDisableButton.frame.size.width, 0.5f)
过潦 2024-12-21 22:45:42

“进行这种旋转的一种更简单的方法是将对象放在对称的 UIView 中,将其置于所需的旋转点中心并分配旋转变换。”这种方式很好。

"An easier way to do this kind of rotations is to put an object within a symmetric UIView, center it at the desired point of rotation and assign a rotation transform." this way is fine.

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