What do borders really mean in CALayer?

I was involved in evaluating the properties of CALayer,

 - (void)viewDidLoad {
    [super viewDidLoad];

    CALayer *sublayer = [CALayer layer];
    sublayer.backgroundColor = [UIColor blueColor].CGColor;
    sublayer.frame = CGRectMake(18, 18, 154, 154);
    [self.view.layer addSublayer:sublayer];

    CALayer *sublayer2 = [CALayer layer];
    sublayer2.backgroundColor = [UIColor redColor].CGColor;
    sublayer2.frame = CGRectMake(20, 20, 150, 150);
    sublayer2.bounds = CGRectMake(0, 0, 50, 50);
    sublayer2.zPosition = 10;

    [self.view.layer addSublayer:sublayer2];
}

sublayer2 draw a small 50X50 rectangle in the center of the rectangle of the sublayer1, but it will draw a 150X150 rectangle if this line is commented out:

sublayer2.bounds = CGRectMake(0, 0, 50, 50);
+3
source share
1 answer

after reading the sch manual, I think the behavior is explained by the following reason:

1 as indicated in the manual

A straight rectangle is expressed in representations of the system's own local coordinate. By default, this rectangle is (0, 0) and its size corresponds to the size of the frame rectangle. // and this is what borders really mean!

......

bounds, .

......

sublayer2.bounds = CGRectMake(0, 0, 50, 50);

50X50,

CGRectMake (0, 0,..) "0,0" , .

2, anchorPoint, anchorPoint (0.5.0.5), (95,95), , , 5X5, (95,95)

, ,

+1

All Articles