iOS開發(fā)常用方法--坐標轉(zhuǎn)換

  1. 點(Point)

    (1)- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

     /*
      * 描述:將坐標點point從view中轉(zhuǎn)換到當前調(diào)用者視圖中,返回在當前視圖中的坐標點
      * 參數(shù):point -> 一個視圖(view)坐標系上的點 
      * 參數(shù):view -> 當前調(diào)用者視圖。如果是圖是nil,那么這個方法將嘗試轉(zhuǎn)換基于窗
      * 口的坐標系。否則視圖和那個接收者必須屬于同一個UIWindow對象。
      * 返回值: 一個轉(zhuǎn)換到接收者(調(diào)用者)坐標系的點 
      */
      
      用法: 
         // controllerA 中有一個UITableView, UITableView里有多行UITableVieCell,現(xiàn)在要把UITableViewCell中的某個點Point轉(zhuǎn)換到controllerA的view中
         // 在controllerA中實現(xiàn):
         CGRect point = [self.view convertPoint:aPoint fromView:cell];
         // 此 point 為在 controllerA 的 view 中的 point
    

    (2)- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;

     /*
      * 描述:將坐標點point由point所在視圖轉(zhuǎn)換到目標視圖view中,返回在目標視圖view中的坐標點
      * 參數(shù):point -> 一個在當前視圖坐標系中的點
      * 參數(shù):view -> 目標視圖對象view。如果視圖是nil,那么這個方法將會轉(zhuǎn)換成基于窗
      * 口的坐標。否則視圖和接收者都要屬于同一個UIWindow對象。 
      * 返回值:一個轉(zhuǎn)換到接收者(view)坐標系的點 
      */
      
       用法: 
         // controllerA 中有一個UITableView, UITableView里有多行UITableVieCell,現(xiàn)在要把UITableViewCell中的某個點Point轉(zhuǎn)換到controllerA的view中
         // 在controllerA中實現(xiàn):
         CGRect point = [cell convertPoint:aPoint toView:self.view];
         // 此 point 為在 controllerA 的 view 中的 point
    
  2. 面(Rect)

    (1)- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;

     /*
      * 描述:將rect從view中轉(zhuǎn)換到當前視圖中,返回在當前視圖中的frame
      * 參數(shù):rect -> 一個視圖在當前所在視圖坐標系中的frame
      * 參數(shù):view -> 當前坐標系視圖對象。如果視圖是nil,那么這個方法將會基于窗口來轉(zhuǎn)換。   
      * 否則視圖和接收者必須都屬于同一個UIWindow對象
      * 返回值:The converted rectangle -> 一個轉(zhuǎn)換過的frame 
      */
      
      用法: 
         // controllerA 中有一個UITableView, UITableView里有多行UITableVieCell,cell上放有一個button,現(xiàn)在要把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到controllerA的view中
         // 在controllerA中實現(xiàn):
         CGRect rect = [self.view convertRect:cell.btn.frame fromView:cell];
         // 或當已知btn時:
         CGRect rect = [self.view convertRect:btn.frame fromView:btn.superview];
         // 此 rect 為 btn 在 controllerA 的 view 中的 rect
    

    (2)- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;

     /*
      * 描述:將rect由rect所在視圖轉(zhuǎn)換到目標視圖view中,返回在目標視圖view中的frame
      * 參數(shù):rect -> 一個視圖在當前所在視圖坐標系中的frame
      * 參數(shù):view -> 要轉(zhuǎn)換過去的目標視圖對象。如果這個是視圖是nil,這個方法將會基于窗口坐標系來轉(zhuǎn)換。
      * 否者視圖和接收者必須屬于同一個UIwindow對象
      * 返回值:The converted rectangle -> 一個轉(zhuǎn)換過的frame
      */
      
      用法:
          // controllerA 中有一個UITableView, UITableView里有多行UITableVieCell,cell上放有一個button,現(xiàn)在要把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到controllerA的view中
         // 在controllerA中實現(xiàn):
         CGRect rect = [cell convertRect:cell.btn.frame toView:self.view];
         // 或當已知btn時:
         CGRect rect = [btn.superview convertRect:btn.frame toView:self.view];
         // 此 rect 為 btn 在 controllerA 的 view 中的 rect
    

參考文章:http://www.cnblogs.com/pengyingh/articles/2379476.html UIView 中常見的方法總結(jié)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容