项目中常有需获取单独某一控件或试图在其父视图中的坐标尺寸的相关需求。
可通过下方方法获取控件在父视图的固定坐标系
以下两种方法相同
CGRect rect=[cell convertRect:cell.textView.bounds toView:self.tableView];
CGRect rect = [self.tableView convertRect:cell.textView.bounds fromView:cell];
层级关系
cell.textView
->
cell
->
self.tableView
(父视图)
而如果要求在页面滑动到某一控件处
或者控件滑动到window页面某一坐标点处
时需显示或进行某些操作,可以将toView
设定为window
,这时在scrollViewDidScroll
中显示的坐标即为控件在当前页面
的坐标
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
CGRect rect=[self.textView convertRect:self.textView.bounds toView:window];
if (rect.origin.y<SCREEN_HEIGHT) {
[self.textView becomeFirstResponder];
}
}
转换像素点
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
[
fromView
convertPoint:point
toView:toView
];
[toView
convertPoint:point
fromView:fromView
];
转换Rect
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
[
fromView
convertRect:rect
toView:toView
];
[toView
convertRect:rect
fromView:fromView
];
网友评论