zoukankan      html  css  js  c++  java
  • frame和bounds

    frame其实也是一个结构体,是CGRect的一个变量。
    //
    //  ViewController.m
    //  UIView01
    //
    //  Created by cqy on 16/2/12.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建View对象
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
        //设置背景颜色
        view1.backgroundColor = [UIColor yellowColor];
        //添加视图
        [self.view addSubview:view1];
        //frame其实也是一个结构体,有两个成员变量均为结构体,两个成员变量分别有两个成员变量都是cgfloat类型的;
        UIView *view2 = [[UIView alloc]init];
        view2.frame = CGRectMake(20, 140, 200, 100);
        view2.backgroundColor = [UIColor greenColor];
        [self.view addSubview:view2];
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
    bounds:在每添加⼀个view的时候,每个view分别以⾃⼰的起点为(0,0)点,创建⼀个坐标系,这个bounds就是⼀个结构体,⽤来确定view的新坐标。 
    //  ViewController.m
    //  UIView01
    //
    //  Created by cqy on 16/2/12.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建View对象
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
        //设置背景颜色
        view1.backgroundColor = [UIColor yellowColor];
        //添加视图
        [self.view addSubview:view1];
        //frame其实也是一个结构体,有两个成员变量均为结构体,两个成员变量分别有两个成员变量都是cgfloat类型的;
        UIView *view2 = [[UIView alloc]init];
        view2.frame = CGRectMake(20, 140, 200, 100);
        view2.backgroundColor = [UIColor greenColor];
        [self.view addSubview:view2];
        //bounds也是结构体,与frame一样。只是坐标是从原点开始;
        UIView *view3 = [[UIView alloc]init];
        view3.bounds = CGRectMake(0, 0, 50, 50);
        view3.backgroundColor = [UIColor redColor];
        [view2 addSubview:view3];
        NSLog(@"view1.bounds:%@",NSStringFromCGRect(view1.bounds));//结果:view1.bounds:{{0, 0}, {200, 100}}
        NSLog(@"view1.frame:%@",NSStringFromCGRect(view1.frame));//结果: view1.frame:{{20, 20}, {200, 100}}
        //center是以视图坐标系为参照,取得中点
        UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(20, 260, 200, 100)];
        view4.backgroundColor = [UIColor brownColor];
        [self.view addSubview:view4];
         NSLog(@"view1.center:%@",NSStringFromCGPoint(view1.center));//结果:view1.center:{120, 70}
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
    frame与bounds的区别:frame与bounds都是结构体,都是⽤来确定位置和⼤⼩的;
    frame的位置是从屏幕的最左上⾓开始算的;    
    bounds是从新添加的view的最左上⾓的点开始算。
  • 相关阅读:
    Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)
    Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
    BZOJ 5495: [2019省队联测]异或粽子 (trie树)
    洛谷【P2669】NOIP2015普及组 T1金币
    解决Win 10上SSD缓慢问题
    如何保障数据安全
    一个网工的linux学习过程
    JS实现select去除option的使用注意事项
    codevs1506传话(kosaraju算法)
    我的园子
  • 原文地址:https://www.cnblogs.com/iQingYang/p/5193167.html
Copyright © 2011-2022 走看看