我不太明白方法convertPoint:toView:
。
在 Apple 的文档中写道
convertPoint:toView:
Converts a point from the receiver’s coordinate system to that of the specified view.
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
但是将一个点从一个点转换为另一个点到底意味着什么?
这是否意味着两个边界中的点具有不同的单位?还是只是值(value)观不同?
如果是后者,为什么我们可以简单地将a的contentOffset
的值赋值给b的,为什么会有这样的方法?
CGPoint a = [a contentOffset];
[b setContentOffset:a];
convertPoint:toView:
与简单分配 contentOffset
有何不同?还是我误解了整个概念? 转换点数的实际作用是什么?什么时候应该使用这种方法?
最佳答案
每个 UIView 都有自己的坐标系。因此,如果您有一个包含另一个 UIView_2 的 UIView_1,则它们中都有一个点 (10,10)。
convertPoint:toView:允许开发人员在一个 View 中取一个点并将该点转换为另一个 View 坐标系。
例子: View 1 包含 View 2。 view2 的左上角位于 view1 点 (10,10),或者更好地说 view2.frame.orgin = {10,10}。 {10,10} 基于 view1 坐标系。到目前为止一切顺利。
用户在 view2 内部的 {20,20} 点触摸了 view2。现在这些坐标在 view2 坐标系中。您现在可以使用 covertPoint:toView: 将 {20,20} 转换为 view1 的坐标系。接触点 = {20,20}
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
所以现在 pointInView1Coords 在 view1 坐标系中应该是 {30,30}。现在这只是这个例子中的简单数学运算,但是有各种各样的事情有助于转换。想到转换和缩放。
阅读有关 UIView 框架、边界和中心的信息。它们都是相关的,它们处理 View 的坐标系。在您开始与他们一起做事之前,它会让人感到困惑。记住这个框架和中心在 parent 的坐标系中。边界在 View 的坐标系中。
约翰
关于ios - 了解 convertPoint :toView:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109958/