0

When i changed the frame of view, and will the frame of layer change with it? Why ?For example .

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; 

[self.view addSubview:testView]; 

NSLog(@"(%f,%f,%f,%f)", testView.layer.frame.origin.x, testView.layer.frame.origin.y, testView.layer.frame.size.width, testView.layer.frame.size.height); 

testView.frame = CGRectMake(200, 200, 100, 100); 

NSLog(@"(%f,%f,%f,%f)", testView.layer.frame.origin.x, testView.layer.frame.origin.y, testView.layer.frame.size.width, testView.layer.frame.size.height);

Output:

2018-03-07 22:13:33.251349+0800 ViewLayer[25622:1982995] layer frame:(100.000000,100.000000,100.000000,50.000000) 
2018-03-07 22:13:33.251483+0800 ViewLayer[25622:1982995] layer frame:(200.000000,200.000000,100.000000,100.000000)
DonMag
  • 69,424
  • 5
  • 50
  • 86

1 Answers1

0

No, the frame a of Layer does not change with the frame of the view.

Why? Because they are used for different tasks, and layers have their own coordinate space.

I suggest you take a read through the documentation - here's a good place to start: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514

If you want a layer's frame to change with the view's frame, you need to subclass that view and override / implement layoutSubviews.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • OK - the view's `layer` is part of the view, so **its*** frame will change with the view. If you `addSublayer` though, that sublayer's frame will not change. – DonMag Mar 07 '18 at 15:02