iOS开发常用方法--坐标转换

作者: 蒲公英少年 | 来源:发表于2016-05-04 10:10 被阅读2896次
    1. 点(Point)

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

       /*
        * 描述:将坐标点point从view中转换到当前调用者视图中,返回在当前视图中的坐标点
        * 参数:point -> 一个视图(view)坐标系上的点 
        * 参数:view -> 当前调用者视图。如果是图是nil,那么这个方法将尝试转换基于窗
        * 口的坐标系。否则视图和那个接收者必须属于同一个UIWindow对象。
        * 返回值: 一个转换到接收者(调用者)坐标系的点 
        */
        
        用法: 
           // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,现在要把UITableViewCell中的某个点Point转换到controllerA的view中
           // 在controllerA中实现:
           CGRect point = [self.view convertPoint:aPoint fromView:cell];
           // 此 point 为在 controllerA 的 view 中的 point
      

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

       /*
        * 描述:将坐标点point由point所在视图转换到目标视图view中,返回在目标视图view中的坐标点
        * 参数:point -> 一个在当前视图坐标系中的点
        * 参数:view -> 目标视图对象view。如果视图是nil,那么这个方法将会转换成基于窗
        * 口的坐标。否则视图和接收者都要属于同一个UIWindow对象。 
        * 返回值:一个转换到接收者(view)坐标系的点 
        */
        
         用法: 
           // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,现在要把UITableViewCell中的某个点Point转换到controllerA的view中
           // 在controllerA中实现:
           CGRect point = [cell convertPoint:aPoint toView:self.view];
           // 此 point 为在 controllerA 的 view 中的 point
      
    2. 面(Rect)

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

       /*
        * 描述:将rect从view中转换到当前视图中,返回在当前视图中的frame
        * 参数:rect -> 一个视图在当前所在视图坐标系中的frame
        * 参数:view -> 当前坐标系视图对象。如果视图是nil,那么这个方法将会基于窗口来转换。   
        * 否则视图和接收者必须都属于同一个UIWindow对象
        * 返回值:The converted rectangle -> 一个转换过的frame 
        */
        
        用法: 
           // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button,现在要把UITableViewCell中的subview(btn)的frame转换到controllerA的view中
           // 在controllerA中实现:
           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所在视图转换到目标视图view中,返回在目标视图view中的frame
        * 参数:rect -> 一个视图在当前所在视图坐标系中的frame
        * 参数:view -> 要转换过去的目标视图对象。如果这个是视图是nil,这个方法将会基于窗口坐标系来转换。
        * 否者视图和接收者必须属于同一个UIwindow对象
        * 返回值:The converted rectangle -> 一个转换过的frame
        */
        
        用法:
            // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button,现在要把UITableViewCell中的subview(btn)的frame转换到controllerA的view中
           // 在controllerA中实现:
           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 中常见的方法总结

    相关文章

      网友评论

      • 大生活家:赞一个,点喜欢了
      • OneKeyV:CGRect point = [self.view convertPoint:aPoint fromView:cell];

        CGRect 这个类型写错了吧,应该是CGPoint

      本文标题:iOS开发常用方法--坐标转换

      本文链接:https://www.haomeiwen.com/subject/bzyrrttx.html