UIImageView坐标到子视图坐标

发布于 2024-10-17 08:27:01 字数 81 浏览 4 评论 0原文

如果我从 UIImageView 开始,并添加一个子视图,如何将原始 UIImageView 中的坐标转换为子视图中相应的坐标(屏幕上的同一位置)?

If I start with a UIImageView, and I add a subview, how do I translate a coordinate in the original UIImageView to a corresponding coordinate (the same place on the screen) in the subview?

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

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

发布评论

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

评论(3

寂寞笑我太脆弱 2024-10-24 08:27:01

UIView 提供了专门用于此目的的方法。根据您的情况,您有两种选择:

CGPoint newLocation = [imageView convertPoint:thePoint toView:subview];

或者

CGPoint newLocation = [subview convertPoint:thePoint fromView:imageView];

它们都做同样的事情,所以选择您认为更合适的一个。还有用于转换矩形的等效函数。这些函数将在同一窗口上的任意两个视图之间进行转换。如果目标视图为零,它将与窗口基坐标进行转换。这些函数可以处理不是彼此直接后代的视图,并且还可以处理带有变换的视图(尽管在包含任何旋转或倾斜的变换的情况下,矩形方法可能不会产生准确的结果)。

UIView provides methods for exactly this purpose. In your case you have two options:

CGPoint newLocation = [imageView convertPoint:thePoint toView:subview];

or

CGPoint newLocation = [subview convertPoint:thePoint fromView:imageView];

They both do the same thing, so pick whichever one feels more appropriate. There's also equivalent functions for converting rects. These functions will convert between any two views on the same window. If the destination view is nil, it converts to/from the window base coordinates. These functions can handle views that aren't direct descendants of each other, and it can also handle views with transforms (though the rect methods may not produce accurate results in the case of a transform that contains any rotation or skewing).

蘸点软妹酱 2024-10-24 08:27:01

从父视图中的点减去子视图的frame.origin到子视图坐标中的同一点:

subviewX = ParentX - subview.frame.origin.x;

subviewY = ParentY - subview.frame.origin.y;

Subtract the subview's frame.origin from the point in the parents view to the same point in the subview's coordinate:

subviewX = parentX - subview.frame.origin.x;

subviewY = parentY - subview.frame.origin.y;

输什么也不输骨气 2024-10-24 08:27:01

从如下代码开始:

UIImageView* superView=....;
UIImageView subView=[
    [UIImageView alloc]initWithFrame:CGRectMake(0,0,subViewWidth,subViewHeight)
];
subView.center=CGPointMake(subViewCenterX, subViewCenterY);
[superView addSubview:subView];

(subViewCenterX, subViewCenterY) 坐标是 superView 中的一个点,其中 subView 的中心被“固定”。子视图可以通过移动其中心来围绕超级视图移动。例如,我们可以将

subView.center=CGPointMake(subViewCenterX+1, subViewCenterY);

其向右移动 1 个点。现在假设我们在 superView 中有一个点 (X,Y),我们想在 subView 中找到对应的点 (x,y),这样 < code>(X,Y) 和 (x,y) 引用屏幕上的同一点。 x 的公式为:

x=X+subViewWidth/2-subViewCenterX;

y 的公式也类似:

y=Y+subViewHeight/2-subViewCenterY;

为了解释这一点,如果您绘制一个表示 superView 的框,并绘制另一个(较大的)框表示 subView,则差异 subViewWidth/2-subViewCenterX 为“子视图框伸出到超级视图左侧的位的宽度”

Starting with code like:

UIImageView* superView=....;
UIImageView subView=[
    [UIImageView alloc]initWithFrame:CGRectMake(0,0,subViewWidth,subViewHeight)
];
subView.center=CGPointMake(subViewCenterX, subViewCenterY);
[superView addSubview:subView];

The (subViewCenterX, subViewCenterY) coordinate is a point, in superView, where the center of subView is "pinned". The subView can be moved around wrt the superView by moving its center around. We can go, for example

subView.center=CGPointMake(subViewCenterX+1, subViewCenterY);

to move it 1 point to the right. Now lets say we have a point (X,Y) in the superView, and we want to find the corresponding point (x,y) in the subView, so that (X,Y) and (x,y) refer to the same point on the screen. The formula for x is:

x=X+subViewWidth/2-subViewCenterX;

and similarly for y:

y=Y+subViewHeight/2-subViewCenterY;

To explain this, if you draw a box representing the superView, and another (larger) box representing the subView, the difference subViewWidth/2-subViewCenterX is "the width of the bit of the subView box sticking out to the left of the superView"

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