IOS-Frameworks-UIKit-UIView.h-frame属性和bounds属性

来源:互联网 发布:c语言 libevent 编辑:程序博客网 时间:2024/04/30 05:36

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本身的坐标系统,默认永远都是00点,除非认为setbounds),而frame的原点却是任意的(相对于父视图中的坐标位置)。

Pasted Graphic.tiff

    每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地坐标系统的关键就是要知道的它的原点(00)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改viewbounds属性可以修改本地坐标系统的原点位置。 


- (void)viewDidLoad {

    [superviewDidLoad];

    

    UIView *view0 = [[UIViewalloc]initWithFrame:CGRectMake(40,40, 300,300)];

    view0.backgroundColor = [UIColorgrayColor];

    [self.viewaddSubview:view0];

    

    UIView *view1 = [[UIViewalloc]initWithFrame:CGRectMake(40,40, 300,300)];

    view1.backgroundColor = [UIColorredColor];

    view1.bounds =CGRectMake(-40, -40,300, 300);

    [self.viewaddSubview:view1];

    

    UIView *view2 = [[UIViewalloc]initWithFrame:CGRectMake(40,40, 200,200)];

    view2.backgroundColor = [UIColoryellowColor];

    [self.viewaddSubview:view2];

}


Pasted Graphic 1.tiff


又有测试代码如下:

- (void)viewDidLoad {

    [superviewDidLoad];

    

    MyView *view0 = [[MyViewalloc]initWithFrame:CGRectMake(40,40, 200,200)];

    view0.backgroundColor = [UIColorgrayColor];

    [self.viewaddSubview:view0];

    NSLog(@"view0:%@",view0);

    

    MyView *view1 = [[MyViewalloc]initWithFrame:CGRectMake(40,40, 200,200)];

    view1.backgroundColor = [UIColorredColor];

    view1.bounds =CGRectMake(-40, -40,200, 200);

    [self.viewaddSubview:view1];

    NSLog(@"view1:%@",view1);

    

    MyView *view2 = [[MyViewalloc]initWithFrame:CGRectMake(40,40, 100,100)];

    view2.backgroundColor = [UIColoryellowColor];

    //[self.view addSubview:view2];

    NSLog(@"view2:%@",view2);

}


Pasted Graphic 2.tiff


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。

0 0
原创粉丝点击