IOS-Frameworks-UIKit-UIView.h-frame属性和bounds属性
2016-07-13 21:54
369 查看
IOS-Frameworks-UIKit-UIView.h-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的原点却是任意的(相对于父视图中的坐标位置)。
每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触
摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地
坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是
window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *view0 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 300,
300)];
view0.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:view0];
UIView *view1 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 300,
300)];
view1.backgroundColor = [UIColor
redColor];
view1.bounds =
CGRectMake(-40, -40,
300, 300);
[self.view
addSubview:view1];
UIView *view2 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view2.backgroundColor = [UIColor
yellowColor];
[self.view
addSubview:view2];
}
又有测试代码如下:
- (void)viewDidLoad {
[super
viewDidLoad];
MyView *view0 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view0.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:view0];
NSLog(@"view0:%@",view0);
MyView *view1 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view1.backgroundColor = [UIColor
redColor];
view1.bounds =
CGRectMake(-40, -40,
200, 200);
[self.view
addSubview:view1];
NSLog(@"view1:%@",view1);
MyView *view2 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 100,
100)];
view2.backgroundColor = [UIColor
yellowColor];
//[self.view addSubview:view2];
NSLog(@"view2:%@",view2);
}
2016-07-13 21:36:33.985 GCD[665:225810] view0:<MyView: 0x12d65ff80; frame = (40 40; 200 200); layer = <CALayer: 0x12d6888d0>>
2016-07-13 21:36:33.987 GCD[665:225810] view1:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>>
2016-07-13 21:36:33.987 GCD[665:225810] view2:<MyView: 0x12d535050; frame = (40 40; 100 100); layer = <CALayer: 0x12d530e50>>
2016-07-13 21:36:42.661 GCD[665:225810] self:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>> locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5}
2016-07-13 21:36:42.774 GCD[665:225810] self:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>> locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5}
点击红色方块左上角发现打印的locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5},说明对bounds的设定影响了UITouch的相对于自己的location。
一、首先列一下公认的资料:
先看到下面的代码你肯定就明白了一些:
-(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的原点却是任意的(相对于父视图中的坐标位置)。
每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触
摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地
坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是
window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *view0 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 300,
300)];
view0.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:view0];
UIView *view1 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 300,
300)];
view1.backgroundColor = [UIColor
redColor];
view1.bounds =
CGRectMake(-40, -40,
300, 300);
[self.view
addSubview:view1];
UIView *view2 = [[UIView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view2.backgroundColor = [UIColor
yellowColor];
[self.view
addSubview:view2];
}
又有测试代码如下:
- (void)viewDidLoad {
[super
viewDidLoad];
MyView *view0 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view0.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:view0];
NSLog(@"view0:%@",view0);
MyView *view1 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 200,
200)];
view1.backgroundColor = [UIColor
redColor];
view1.bounds =
CGRectMake(-40, -40,
200, 200);
[self.view
addSubview:view1];
NSLog(@"view1:%@",view1);
MyView *view2 = [[MyView
alloc]initWithFrame:CGRectMake(40,
40, 100,
100)];
view2.backgroundColor = [UIColor
yellowColor];
//[self.view addSubview:view2];
NSLog(@"view2:%@",view2);
}
2016-07-13 21:36:33.985 GCD[665:225810] view0:<MyView: 0x12d65ff80; frame = (40 40; 200 200); layer = <CALayer: 0x12d6888d0>>
2016-07-13 21:36:33.987 GCD[665:225810] view1:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>>
2016-07-13 21:36:33.987 GCD[665:225810] view2:<MyView: 0x12d535050; frame = (40 40; 100 100); layer = <CALayer: 0x12d530e50>>
2016-07-13 21:36:42.661 GCD[665:225810] self:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>> locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5}
2016-07-13 21:36:42.774 GCD[665:225810] self:<MyView: 0x12d5335e0; frame = (40 40; 200 200); layer = <CALayer: 0x12d506680>> locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5}
点击红色方块左上角发现打印的locationInSelf:{-34.5, -39.5} locationInWindow:{45.5, 40.5},说明对bounds的设定影响了UITouch的相对于自己的location。
相关文章推荐
- [LeetCode] Guess Number Higher or Lower
- HDU 1242 Rescue (第一道优先队列+BFS)
- An AnnotationConfiguration instance is required to use...等几个异常
- QueryTask查询结果最多500条的问题
- js信息滚动marquee标签
- UI基础_UIImageView的contentMode总结
- android studio 新建项目 界面一直停在 【“building ‘ 项目名’ gradle project info”】
- Qt之保持GUI响应
- Qt之保持GUI响应
- Qt之保持GUI响应
- 编译内核提示mkimage command not found – U-Boot images will not be built
- 【Android自助餐】Handler消息机制完全解析(二)MessageQueue的队列管理
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
- AbstractQueuedSynchronizer(六)——release
- WeX5的简单介绍及UI的简单讲解
- android stuido常见问题查询网址(此贴不定期更新)
- 关于UITextFiled,UIlabel,UIBUtton相关设置,边框设置和字体设置
- __builtin_return_address(LEVEL)函数解释
- UITableView的性能优化
- UE4中的DynamicTexture数据更新