Giter VIP home page Giter VIP logo

jwcocoademo's Introduction

#IOS View的Frame 、bounds 和 center的区别(位置和大小)

先看下下面这段代码

-(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)点(就是view本身的坐标系统,默认永远都是0,0点,除非认为setbounds),而frame的原点却是任意的(相对于父视图中的坐标位置)。

再来看张图就明白了

Editor preferences pane

frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统) **bounds:**该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)
**center:**该view的中心点在父view坐标系统中的位置和大小。(参照电是,父亲的坐标系统)

我个人认为,bounds稍微有点费解,稍不留神,想的多了,就会绕进去。每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触 摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地 坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。

所以,我个人认为,bounds影响到子view的位置和大小。

#demo演示:

UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];
parentView.bounds = CGRectMake(-20, -20, 280, 250);
parentView.backgroundColor = [UIColor grayColor];
[self.view addSubview:parentView];
    
UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
childView.backgroundColor = [UIColor yellowColor];
[parentView addSubview:childView];
    
NSLog(@"parentView frame :%@ ###### parentView bounds :%@",NSStringFromCGRect(parentView.frame),NSStringFromCGRect(parentView.bounds));
NSLog(@"childView frame :%@ ###### childView bounds :%@",NSStringFromCGRect(childView.frame),NSStringFromCGRect(childView.bounds));

Iphone run (运行展示,图中说的很明白了哦) ###运行结果如下 运行结果

(log输出日志表明,每个新的view默认的bounds其实都是(0,0))

jwcocoademo's People

Stargazers

Jack Wong avatar

Watchers

James Cloos avatar Jack Wong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.