UIView相對座標轉換

2021-07-03 22:09:43 字數 887 閱讀 6011

有時候要在view裡,要求a相對於b的位置,可用uiview提供的以下函式:

convertrect:toview: 

- (cgrect)convertrect:(cgrect)rect toview:(uiview *)view

轉換呼叫者檢視中的某個矩形,轉換為相對於view的矩形座標

convertrect:fromview: 

轉換乙個矩形從其他檢視座標系到呼叫者座標系。

下面是兩個點的轉換,與上面的矩形轉換一樣。

convertpoint:toview: 

convertpoint:fromview: 

下面看個圖更加明顯,在控制器的view裡面有兩個控制項,乙個view a,乙個view b,如圖所示:

其中a的frame為,,},b 的frame為,}

為了方便,兩個view的x座標是一樣的。然後求a相對於b的座標位置,就可以呼叫他們父view的方法,求a相對於b的座標位置

cgrect  viewaframw = self.viewa.frame;

cgrect  viewa2viewbframw =  [self.view convertrect:viewaframe toview:viewb ];

那viewa2viewbframw  的值就等於,},由於是相對於b的座標,一般情況b的的

bounds

原點是(0,0).所以做了轉換之後,a的x座標是0,而y的座標就是他們在父view裡y座標值的相差的絕對值,那即是-60,所以viewa 相對於viewb是的矩形座標為

,}.其他3個函式也是類似,有了這幾個方法可以快速算出相對座標,不用再自己轉換了。

UIView中的座標轉換

今天使用cgrectcontainsrect方法時,出現了一些問題,現在總結一下 首先了解一下一些基本的方法 cgrectcontainsrect cgrect rect1,cgrect rect2 判斷rect1是否包含rect2 cgrectcontainspoint cgrect rect,c...

UIView中的座標轉換

將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 cgpoint convertpoint cgpoint point toview uiview view 將畫素point從view中轉換到當前檢視中,返回在當前檢視中的畫素值 cgpoint conv...

UIView中的座標轉換

將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 cgpoint convertpoint cgpoint point toview uiview view ...