Objective-C基础

iOS 坐标概念 frame、bounds、anchorPoin

2019-06-26  本文已影响0人  3c32f188d5a4

一个面试题让我去深入了解这些东西,之前真的很懵
面试题如下:写出各代码位置 aview bView 的frame 和bounds

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    aview.backgroundColor = [UIColor redColor];

     UIView * bView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    bView.backgroundColor = [UIColor yellowColor];
    [aview addSubview:bView];
   //  aview bView frame    bounds

    aview.transform = CGAffineTransformMakeScale(2, 2);
   //  aview bView frame    bounds

    [self.view addSubview:aview];

    aview.layer.anchorPoint = CGPointMake(0, 0);
   //  aview bView frame    bounds

}

frame

bounds

anchorPoint

position

*position 是layer中的anchorPoint点 在superLayer中的位置坐标,因此可以说position点是相对superlayer的,anchorPoint是相对layer的。两者是相对不同的左边空间的一个重合点

研究代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // 在控制器的视图被加载到内存后调用。
    NSLog(@"%s", __FUNCTION__);
    UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // a frame (100, 100, 100, 100)  bounds  (0, 0, 100, 100)
    aview.backgroundColor = [UIColor redColor];
    // anchorPoint 为0.5 0.5 transform 改变frame 不改变bounds
    aview.transform = CGAffineTransformMakeScale(2, 2);
    // aview 变大宽高变为200 中心点没变对应的x,y 为 50 50   a frame (50, 50, 200, 200)  bounds  (0, 0, 100, 100)
    [self.view addSubview:aview];
    
    UIView * bView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    bView.backgroundColor = [UIColor yellowColor];
    // bview frame bounds 始终没变 (0, 0, 50, 50)
    [aview addSubview:bView];
    
    // 把 anchorPoint 变为 (0,0)的意思是transform 以(0,0)点做transform操作 此时bounds不变 frame改变
    aview.layer.anchorPoint = CGPointMake(0, 0);
    //    position.x = frame.origin.x + curr.anchorpoint.x * curr.frame.size.width
    //    position.y = frame.origin.y + curr.anchorpoint.y * curr.frame.size.height

    //    CGFloat x = aview.layer.position.x - aview.layer.anchorPoint.x *aview.frame.size.width;
    //    CGFloat y = aview.layer.position.y - aview.layer.anchorPoint.y *aview.frame.size.height;
    //    aview 变大宽高变为200 anchorPoint变为(0,0)  a frame (150, 150, 200, 200)  bounds  (0, 0, 100, 100)

}

参考资料 (我这个只是记录下自己用想了解具体内容的看下方连接写的很详细 )

https://www.jianshu.com/p/f5787a1e1d74 关于position计算方面有部分错误
https://www.jianshu.com/p/0658e766a627 解释了bounds 变与不变

上一篇下一篇

猜你喜欢

热点阅读