[ios中uiview的framebounds]Ios中UIView的frame、bounds跟center属性区别

更新时间:2020-03-14    来源:ios    手机版     字体:

【www.bbyears.com--ios】

先看几个概念

坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y

 大小Size:由宽度width和高度height构成,表示一个矩形

区域Rect:它有坐标点Poit和大小Size构成,表示一个区域,既有位置也有大小

相对:有参照物,因参照物的大小位置改变而改变

绝对:无参照物,大小位置固定

再看代码构成:

点由这样来创建,X轴大小和Y大小 CGPoint point = CGPointMake(80,40)

大小Size由这样来创建  CGSize size = CGSizeMake(144,72)表示创建一个宽度为144和高度为72的矩形视图

区域rect这样来创建:CGRect rect=CGRectMake(10, 10, 120, 100);  它表示位置在点(10,10)宽度120高度100的视图

然后呢 看一下frame、bounds跟center三者的定义描述

frame:描述当前视图在其父视图中的位置和大小

bounds:描述当前视图在其自身坐标系统中的位置和大小。

center:描述当前视图的中心点在其父视图中的位置。

我们从描述中可以看出,frame和bounds属性, 都描述视图的大小(CGSize)和位置(CGPoint)的,两者都用CGRect表示。不同的是,frame描述的是在其父视图中的CGRect,而bounds描述的是在其自身视图中的CGRect,也就是说,两者所在的坐标是不同的.而center表示矩形中心点在其父视图中的位置。frame一般用来来设置视图的大小和位置,用center来改变(移动)视图的位置。frame和center都可以改变位置,如果对视图进行旋转、缩放也都是相对于center来操作的。

最直观的我们用一个图和代码值来描述三者的表示:

首先建一个空视图,拖拽三个wiew进去,分别给于不同的颜色,并且命名。代码和图如下


//
//  ViewController.h
//  NSPredicateTest
//
//  Created by xuhongjiang on 15/10/27.
//  Copyright (c) 2015年 xuhongjiang. All rights reserved.
//
#import 
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIView *orangeView;
@property (retain, nonatomic) IBOutlet UIView *greenView;
@property (retain, nonatomic) IBOutlet UIView *grayView;
@end



分别定义了三个view,橙色view最大,里面包含灰色view和绿色view。我们拿绿色view来说明问题。

1.frame

其实灰色view的大小签好就是绿色view的frame大小,绿色视图frame的origin点(灰色view的宽度,灰色view的高度),也既是灰色view相对于橙色view右下角的点,也是绿色view左上角相对于橙色view的点。frame但Size就是绿色view的宽度和高度

2.bounds

绿色view的bounds的点是相对自己,是(0,0),大小size是绿色view的宽度和高度,这个很容易理解。

3.center

绿色view的center的横坐标=(绿色view宽度width/2)+(绿色view的.frame.origin.x);纵坐标=(绿色view高度height/2)+(绿色view的.frame.origin.y);

下面的代码是经过调试后的具体值,仔细观察,不难发现frame、bounds、center三者的区别还是很明显的。

//
//  ViewController.m
//  NSPredicateTest
//
//  Created by xuhongjiang on 15/10/27.
//  Copyright (c) 2015年 xuhongjiang. All rights reserved.
//
#import "ViewController.h"
#import "Products.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self layTest];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void) layTest
{
    //橙色view的frame 返回一个相对于父容器的区域CGRect,由点point和大小Size构成
    CGRect  orangeFrameRect=_orangeView.frame;//(CGRect) orangeRect = (origin = (x = 43, y = 98), size =(width = 289,height = 443))
       CGPoint orangeFramePoit=_orangeView.frame.origin;//(CGPoint) orangePoit = (x = 43, y = 98)
       CGSize  orangeFrameSize=_orangeView.frame.size;//(CGSize) orangeSize = (width = 289, height = 443)
    //橙色view的bounds 返回在自身坐标中的位置和大小,也是一个区域CGRect,同样由点point和大小Size构成
    CGRect orangeBoundsRect= _orangeView.bounds;//(CGRect) orangeBoundsRect = (origin = (x = 0, y = 0), size = (width = 289, height = 443))
    CGPoint orangeBoudsPoit=_orangeView.bounds.origin;//(CGPoint) orangeBoudsPoit = (x = 0, y = 0)
    CGSize orangeBoundsSize=_orangeView.bounds.size;//(CGSize) orangeBoundsSize = (width = 289, height = 443)
    //橙色view的center 它表示相对于父容器的自身中心点
    CGPoint oranageCenterPoit= _orangeView.center;//(CGPoint) oranageCenterPoit = (x = 187.5, y = 319.5)
    
    
    //绿色view的frame 等同于灰色view区域 返回一个相对于父容器(橙色view)的区域CGRect,由点point和大小Size构成
    CGRect  greenFrameRect=_greenView.frame;//(CGRect) greenFrameRect = (origin = (x = 40, y = 64), size = (width = 209, height = 256))
    CGPoint greenFramePoit=_greenView.frame.origin;//(CGPoint) greenFramePoit = (x = 40, y = 64)
    CGSize  greenFrameSize=_greenView.frame.size;//(CGSize) greenFrameSize = (width = 209, height = 256)
    //绿色view的bounds 返回在自身坐标中的位置和大小,也是一个区域CGRect,同样由点point和大小Size构成
    CGRect  greenBoundsRect= _greenView.bounds;//(CGRect) greenBoundsRect = (origin = (x = 0, y = 0), size = (width = 209, height = 256))
    CGPoint greenBoudsPoit=_greenView.bounds.origin;//(CGPoint) greenBoudsPoit = (x = 0, y = 0)
    CGSize  greenBoundsSize=_greenView.bounds.size;//(CGSize) greenBoundsSize = (width = 209, height = 256)
    //绿色view的center 它表示相对于父容器(橙色view)的自身中心点
    CGPoint greenCenterPoit= _greenView.center;//(CGPoint) greenCenterPoit = (x = 144.5, y = 192)
    //灰色view的区域等同于绿色view的frame   x=width=40 y=height=64
    CGSize grayFramePoit=_grayView.frame.size;//(CGSize) grayFramePoit = (width = 40, height = 64)
    //它为什么只是20和32的点  因为它是相对于绿色容器的自身中心点
    CGPoint grayCenterPoit= _grayView.center;//(CGPoint) grayCenterPoit = (x = 20, y = 32)
  }



UIView中bounds和frame的差别


搞iOS开发的童鞋基本都会用过UIView,那他的bounds和frame两个属性也不会陌生,那这两个有什么实质性的区别呢?
 

先看到下面的代码你肯定就明白了一些:
-(CGRect)frame{
    return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect)bounds{
    return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}
很明显,bounds的原点是(0,0)点,而frame的原点却是任意的。


再来看张图,你就会更清楚了。


uiview <wbr><wbr>frame,bounds,center的差异


基本概念:
        frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
        bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统)
        center:该view的中心点在父view坐标系统中的位置和大小。(参照电是,父亲的坐标系统)
实际上只有bounds和center两个属性。frame是为了方便直观多加的属性。所以修改了一个可能会影响到其他属性。文档中如下说:
        Although you can set the values of these properties independently, setting the value for one changes the others in t
he following ways:
        When you set the frame property, the size of the bounds property is set to match the size of the frameproperty. The center property is also adjusted to match the center point of the new frame.
        When you set the center property, the origin of the frame changes accordingly.
        When you set the size of the bounds rectangle, the size of the frame rectangle changes to match。

前 两个很明显,最后一个bounds稍微有点费解。这里一定要顺便说下本地坐标系统:每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触 摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地 坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。

修改bounds测试结果:
        bounds (0,0,100,100) --> (0,0,200,200) 本地坐标系统原点往左往上分别50。center不变,效果是按照中心放大view
        bounds(0,0,100,100) --> (100,100,100,100) 本地坐标系统原点往左往上分别100。center不变,没有可视效果变化(但是本地坐标系统的原点已经改变)
        
结论:
        bounds属性影响到本地坐标系统的原点。需要注意

本文来源:http://www.bbyears.com/shoujikaifa/86963.html

热门标签

更多>>

本类排行