当我改变视框时,图层的框架会随之变化



当我改变视框时,图层的框架会随之改变吗?为什么?例如 .

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);

输出:

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)

不,图层的框架 a 不会随视图的框架而变化。

为什么?因为它们用于不同的任务,并且图层有自己的坐标空间。

我建议您通读文档 - 这是一个很好的起点:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514

如果您希望图层的框架随视图的框架而变化,则需要对该视图进行子类化并覆盖/实现layoutSubviews

最新更新