您的位置:首页 > 产品设计 > UI/UE

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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: