简体   繁体   English

UIImageView坐标到子视图坐标

[英]UIImageView coordinate to subview coordinates

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

UIView provides methods for exactly this purpose. UIView为此提供了一些方法。 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. 还有用于转换rect的等效功能。 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. 如果目标视图为nil,则将其转换为窗口基准坐标或从其转换为窗口基准坐标。 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). 这些函数可以处理彼此之间不是直接后代的视图,还可以处理带有变换的视图(尽管在包含任何旋转或倾斜的变换的情况下,rect方法可能无法产生准确的结果)。

Subtract the subview's frame.origin from the point in the parents view to the same point in the subview's coordinate: 从父视图中的点减去子视图的frame.origin到子视图坐标中的同一点:

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

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

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". (subViewCenterX, subViewCenterY)坐标是subView中“固定” subView中心的subView 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. 向右移动1点。 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. 现在让我们说我们在超级视图中有一个点(X,Y) ,我们想在子视图中找到对应的点(x,y) ,因此(X,Y)(x,y)指向相同的点点在屏幕上。 The formula for x is: x的公式为:

x=X+subViewWidth/2-subViewCenterX;

and similarly for y: 对于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" 为了说明这一点,如果您绘制一个表示SuperView的框,然后绘制另一个(较大的)代表SubView的框,则区别subViewWidth/2-subViewCenterX是“突出到SuperView左侧的SubView框的位的宽度”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM