bounds和frame
来源:互联网 发布:cf显示网络出现异常 编辑:程序博客网 时间:2024/06/02 02:36
面试中可能会遇到各种各样的问题,有时候的细节就容易被忽略,以前没有开始写过博客,面试也总有人问,索性就开始写一个,主要不是为了技术,因为自己的水平也有限度,算是一个总结,碰到类似的事情不会在出错而已。开始做开发的时候,总是想当然的认为这么写一定是正确的,有时候总是知其然而不知其所以然,对于frame和bounds就是,容易混淆。一些资料显示:
-(CGRect)frame{
return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect)bounds{
return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}
很明显,bounds的原点是(0,0)点(就是view本身的坐标系统,默认永远都是0,0点,除非认为setbounds),而frame的原点却是任意的(相对于父视图中的坐标位置)。
frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)
我个人认为,bounds容易出错。每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触 摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地 坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。
所以,我个人认为,bounds影响到子view的位置和大小,列子如下:重新设置坐标系,改变位置的仅仅是子视图的位置,
“`
UIView *fatherView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];
[fatherView setBounds:CGRectMake(-50, -50, 280, 250)];
fatherView.backgroundColor = [UIColor redColor];
[self.view addSubview:fatherView];//添加到self.view
NSLog(@”view1 frame:%@========view1 bounds:%@”,NSStringFromCGRect(fatherView.frame),NSStringFromCGRect(fatherView.bounds));
UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];childView.backgroundColor = [UIColor yellowColor];[fatherView addSubview:childView];//添加到view1上,[此时view1坐标系左上角起点为(-50,-50)]NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(childView.frame),NSStringFromCGRect(childView.bounds));
“`
许多内容,参考其他博客,如有错误在更正。。。
- bounds和frame区别
- ios bounds 和 frame
- frame.bounds和center
- addSubview,frame和bounds
- bounds和frame区别
- bounds和frame区别
- bounds 和frame区别
- frame 和 bounds 区别
- frame、bounds和center
- bounds和frame
- frame,bounds和center
- ios frame和bounds
- Frame和bounds详解
- frame 和 bounds IOS
- frame.center和bounds
- ios-frame和bounds
- iOS frame和Bounds 以及frame和bounds区别
- iOS 一一 frame和Bounds 以及frame和bounds区别
- OpenGL(2)——第一个 OpenGL 程序
- 现代优化算法 之 模拟退火
- Linux 常用命令
- ps的一些基本知识
- iOS PCM文件转MP3格式
- bounds和frame
- 12332
- div滑动效果
- gradle详解
- android 混淆打包
- Android多媒体应用
- 新人
- Xcode自定义Eclipse中常用的快捷键
- 论贝叶斯分类、决策树分类、感知器分类挖掘算法的优势与劣势,以及解决维度效应的策略