繁体   English   中英

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

[英]When i changed the frame of view, and will the frame of layer change with it?

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

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)

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

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

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

如果要使图层的框架随视图的框架而变化,则需要对该视图进行子类化并重写/实现layoutSubviews

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM