iOS开发bug以及解决方案

UIView中的坐标转换

2015-11-22  本文已影响789人  小黄人写代码

大部分内容转自
http://greenchiu.github.io/blog/2014/09/01/memo-the-uiview-convertrect/

newFrame = [viewB convertRect:viewC.frame toView:viewA];
image
在将C直接转移到A后(CAsubView),我们想把B加入到C里面,视觉位置依然保持不变,这时Bframe是对应A的,想要取得B在C应该有的位置,就使用convertRect:fromView
newFrame = [viewC convertRect:viewB.frame fromView:viewA];
image
以下是一些常用的转换方法:
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;

// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;

// 将rect从view中转换到当前视图中,返回在当前视图中的rect
// 例如把UITableViewCell中的subview(btn)的frame转换到VC中
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例如:controllerA中有一个UITableView,UITableView里有多行UITableVieCell,cell上放有一个button
// 在controllerA中实现:
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或者
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
上一篇 下一篇

猜你喜欢

热点阅读