//Converts a rectangle from the receiver’s coordinate system to that of another view
[view1 convertRect:view2.frame toView:view3]
注意:view1是view2的父视图,即view2的frame是相对于view1的(view1一般是scrollview)
view3这里认为是view1的父视图,可以得到滚动过程中view2相对于view3的frame
//Converts a rectangle from the coordinate system of another view to that of the receiver.
[view3 convertRect:view2.frame fromView:view1]这么写和上面代码的结果是一样的
(1) [self.view convertRect:(CGRect) fromView:(nullable UIView *)]; (2) [self.view convertRect:(CGRect) toView:(nullable UIView *)];
上面两行代码,不知道大家了解多少,是否会经常用到。也许你们用到的不多,但是肯定不会陌生,至少曾经在xxx个地方看见过,可一时半会儿又想不起来在哪里见过。于是乎,陷入了纠结ing......
上网查资料,发现千篇一律,而且还比较抽象,不是很容易理解(也许有好的博客,只不过我没发现而已)。废话不多说,这里我用最笨的、也是最通俗易懂的方法来解释下,这两行代码具体的作用。若有不对之处,还望指正!
这两行代码,均可划分为三部分,即:源、目标、被操作的对象。fromView后面接的参数是:源,toView后面接的参数是:目标,convertRect后面接的参数永远是:被操作的对象。作用是:计算源上的被操作的对象相对于目标的frame。举个栗子:
��(1):[viewB convertRect:viewC.frame toView:viewA];
该例子中显然viewA是目标,viewC是被操作的对象,那么剩下的viewB自然而然就是源了。作用就是计算viewB上的viewC相对于viewA的frame。
��(2):[viewC convertRect:viewB.frame fromView:viewA];
该例子viewA是源,viewB是被操作的对象,那么viewC就是目标。作用就是计算viewA上的viewB相对于viewC的frame。
链接:http://www.jianshu.com/p/103d6aac84b4