UIView中的坐标转换convertPoint:convertRect:
发布日期:2021-11-22 04:28:58
浏览次数:2
分类:技术文章
本文共 810 字,大约阅读时间需要 2 分钟。
使用convertPoint或者convertRect进行坐标转换时,比较重要的是使用convertRect:toView:时,主语一定是view.superView,也就是一个子view的父视图,(反过来convertRect:fromView:也一样)如果直接用子view做主语,在使用转换后的frame做绘制时,会报错,如果引用转换后的frame时,位置是不准的,切记!
例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
- // 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];
- // 此rc为btn在controllerA中的rect
- 或当已知btn时:
- CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
- 或
- CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
- //如果以上方法写成
- CGRect rc = [btn convertRect:btn.frame toView:self.view];
- CGRect rc = [self.view convertRect:btn.frame fromView:btn];
- //就会报错,或者得到的新frame是错误的
转载地址:https://blog.csdn.net/zxwcool123/article/details/50767983 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月10日 13时36分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
彻底理解正向代理和反向代理
2019-04-28
一次频繁Full GC的排查过程,根源居然是它...
2019-04-28
【爬虫】简单十行代码爬取B站的B站弹幕
2019-04-28
Servlet 体系结构
2019-04-28
Java笔记—语法
2019-04-28
Java笔记—数组
2019-04-28
Java笔记—继承
2019-04-28
MySQL的数据类型
2019-04-28
洛谷 P1886 滑动窗口 /【模板】单调队列
2019-04-28
洛谷 P3367 【模板】并查集
2019-04-28
【算法学习】高级数据结构2 种类并查集
2019-04-28
洛谷 P1525 关押罪犯【种类并查集】
2019-04-28
洛谷 P2024 [NOI2001]食物链【种类并查集】
2019-04-28
POJ 1703 Find them, Catch them【种类并查集】
2019-04-28
POJ 2492 A Bug‘s Life【种类并查集】
2019-04-28
POJ 2236 Wireless Network【并查集】
2019-04-28
LeetCode C++ 214. Shortest Palindrome【字符串】困难
2019-04-28
洛谷 P2580 于是他错误的点名开始了【字典树/Map】
2019-04-28
HDU 3336 Count the string【KMP的next数组性质】
2019-04-28