简述
Frame: 视图的方位和巨细运用是父视图的坐标系,所以将视图放置在父级中这一点就很重要。 Bounds:视图的方位和巨细,运用的是其自己的坐标系,而对于这一点而言将视图的内容或子视图放置在其本身内很重要。
frame和bounds
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[superView addSubview:imageView];
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779601-3328fa951ef9bea.png)
Frame origin = (0, 0) width = 100 height = 100
Bounds origin = (0, 0) width = 100 height = 100
该图片中的Frame
和Bounds
彻底相同。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
[superView addSubview:imageView];
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779606-32d7caf9ddedc9e.png)
Frame origin = (10, 50) width = 100 height = 100
Bounds origin = (0, 0) width = 100 height = 100
改动Frame的 x、y 坐标会在父视图中移动它。可是视图本身看起来依然彻底相同。Bounds并没有不同。到目前为止,咱们看到的Frame
和Bounds
的宽度和高度一直是彻底相同的。然而这并不总是正确的。下面看看假如旋转视图片会产生什么。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
[superView addSubview:imageView];
imageView.transform = CGAffineTransformMakeRotation(M_PI * 0.05);
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779612-11ccea6aea59ad1.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779618-cb582d74607ceb9.png)
Bounds
依然相同,可是Frame
已经产生了更改。现在更简单看出frame
和bounds
之间的差异。
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779624-83a99f9ea159000.png)
Note: When modifying the transform property of your view, all transformations are performed relative to the center point of the view.
,修正transform
视图的属性时,所有转化都是相对于视图的中心点履行的。也就是说,咱们进行transform
后,再去修正视图方位最好运用center
属性来修正。
到目前为止,Bounds原点是一直停留在 (0, 0)的,不过也纷歧定是这样的。假如咱们的视图有一个很大的子视图,它太大而无法一次显现呢?
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 120, 200)];
[self.view addSubview:view2];
view2.clipsToBounds = YES;
// view2.bounds = CGRectMake(50, 0, 120, 200);
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(-50, 50, 200, 200)];
[view2 addSubview:imageView2];
[imageView2 setImage:[UIImage imageNamed:@"pic.jpg"]];
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779630-97e6b8680d4358e.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779636-63dadbdee4a65ab.png)
view2.bounds = CGRectMake(50, 0, 120, 200);
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779643-cf5a1ff5ed3fb79.png)
![[iOS开发]frame和bounds [iOS开发]frame和bounds](https://www.6hu.cc/wp-content/uploads/2023/06/1686779649-29e822609ac96c0.png)
UIScrollView
系列控件的思维是相同的。大的子视图相当于画布,改动bounds
更改的是显现画布的区域。
何时运用Frame,何时运用Bounds
因为
frame
相关视图在其父视图中的方位,因而您在进行向外更改时会运用它,例如更改其宽度或查找视图与其父视图顶部之间的间隔。
运用
bounds
时,你正在向内改变,就像画的东西或视图中组织子视图。假如您对它进行了一些转化,还能够运用bounds
来获取视图的巨细。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。