iOS AnchorPoint 引起的坐标问题
这里主要讨论设置AnchorPoint 改变时,会影响我们预期的布局问题;
一、初始代码布局
//参照页面
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
UIView *bView = [[UIView alloc]initWithFrame:CGRectMake(150, 50, 100, 100)];
bView.backgroundColor = [UIColor blueColor];
[self.view addSubview:bView];
运行效果如下(默认anchorPoint(0.5,0.5))
其中 B 坐标信息 :
frame : (origin = (x = 150, y = 50), size = (width = 100, height = 100))
center :(x = 200, y = 100)
二、更改B页面 anchorPoint为 (0,0.5)
bView.layer.anchorPoint = CGPointMake(0.0, 0.5);
运行,结果如下:
B 坐标信息如下
frame : (origin = (x = 200, y = 50), size = (width = 100, height = 100))
center : (x = 200, y = 100)
和“一 ”中结果 相比发现,center不变, x坐标向右移动了50pt;
三、更改B页面 anchorPoint为 (0.0,0.0)
bView.layer.anchorPoint = CGPointMake(0.0, 0.0);
运行,结果如下:
B 坐标信息如下
frame : (origin = (x = 200, y = 100), size = (width = 100, height = 100))
center : (x = 200, y = 100)
此时发现 origin 和center重叠了
四、结论
我们设置anchorPoint 时已经改变了我们的布局,这不是我们所期望的;
从二、三运行结果可以发现一下结论:
- center坐标的值都未改变。
- center 点即使我们的锚点
- 我们改变锚点值时,页面会相对于原中心点进行相应改变
五、解决方案
那么我们如何想要得到:只设定anchorPoint值,而不影响页面布局呢? 答案是,重新计算frame值;
代码如下:
/**设置锚点,且不影响之前的预期布局@param anchorPoint 锚点值如:{1,0.5}@param view 要更改的view*/ - (void)setViewAnchorPoint:(CGPoint)anchorPoint forView:(UIView*)view {CGPoint originAnchorPoint = view.layer.anchorPoint;CGPoint offetPoint = CGPointMake(anchorPoint.x - originAnchorPoint.x, anchorPoint.y - originAnchorPoint.y);CGFloat offetX = (offetPoint.x)*view.frame.size.width;CGFloat offetY = (offetPoint.y)*view.frame.size.height;view.layer.anchorPoint = anchorPoint;//设置这个值 说明已经改变了便宜量view.layer.position = CGPointMake(view.layer.position.x + offetX, view.layer.position.y + offetY);//将指定的偏宜量更改回来 }
那么我们设置需要更改的B页面如下调用即可:
UIView *bView = [[UIView alloc]initWithFrame:CGRectMake(150, 50, 100, 100)];_bView = bView;bView.backgroundColor = [UIColor blueColor]; [self setViewAnchorPoint:CGPointMake(1, 1) forView:bView];//只是测试多次连续设置结果[self setViewAnchorPoint:CGPointMake(0, 0.5) forView:bView];[self.view addSubview:bView];
完美结局
转载于:https://www.cnblogs.com/kingbo/p/7162364.html
iOS AnchorPoint 引起的坐标问题相关推荐
- iOS开发基础-九宫格坐标(4)
对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...
- 在IOS中根据圆心坐标、半径和角度计算圆弧上的点坐标
/** 日期:2015-10-15 版本: 1.0.0 -------------------------------------------------------------- 功能说明 ---- ...
- ios如何获取gps坐标(定位第二节)
原文连接: https://blog.csdn.net/michael_ouyang/article/details/54378338 -------------------------------- ...
- appium ios 上滑y坐标传值问题
问题 按照android的滑动,比如要是向上滑动就是y坐标从大滑到小,比如y坐标是100,那么相对坐标就是100*0.8->100*0.2,x坐标不变即可,这样子在android上面运行没问题, ...
- ios 解决有关火星坐标的问题
CLLocationManager 定位获取CLLocation 是地球坐标,而MKMapView 获取的是天朝火星坐标,这就导致了使用定位的时候,显示用户的蓝点和当前定位信息偏差(因为蓝点是调用了M ...
- ios绘图时的坐标处理
在iOS中,进行绘图操作时,一般主要是在UIView:drawRect中调用 UIGraphicsBeginImageContextWithOptions等一系列函数,有时候直接画图就行,比如UIIm ...
- IOS 根据地理位置获取坐标或根据坐标获取地理位置
根据地理位置获取坐标: CLGeocoder * grader =[[CLGeocoder alloc]init]; [groder geocodeAddressString:@"要获取坐标 ...
- iOS的layer的anchorpoint与posion问题
每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,分别为frame.bounds.position.a ...
- 图层几何学 -- iOS Core Animation 系列二
<图层树和寄宿图 -- iOS Core Animation 系列一>介绍了图层的基础知识和一些属性方法.这篇主要内容是学习下图层在父图层上怎么控制位置和尺寸的. 1.布局 首先看一张例图 ...
- 彻底理解position与anchorPoint - Wonderffee's Blog(转)
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...
最新文章
- 协程Coroutines入门
- jquery中在子窗口中获取父窗口的Input文本值
- Windows Server 2008 升级安装
- 高并发场景之RabbitMQ篇
- Openstack nova-scheduler 源码分析 — Filters/Weighting
- Scala 内部类及外部类
- html5+css3网页开发实战精解,HTML5+CSS3开发实战
- 20189215 2018-2019-2 《密码与安全新技术专题》第7周作业
- python操作hive数据库代码_Python连接Hive操作数据库
- 使用FastReport报表工具生成图片格式文档
- 【Computer Organization笔记04】ALU的基本功能,1位ALU,位数扩展以及功能扩展
- 最新搭建GIT服务器仓库
- Feedforward Deep Networks(要点)
- 【转】spring MVC入门示例(hello world demo)
- XamarinEssentials教程应用程序信息AppInfo
- Photoshop安装:详细安装步骤
- Spring注解汇总
- PPT写的好,升职加薪不会少-全套免费教程
- 服务器hdmi如何连接显示器,容易被忽略的HDMI连接显示器的一个重要设置
- 《中国合伙人》:致我们即将逝去的青春