• iOS中frame和bounds之间的区别



    先看下官方以及个人翻译:

    frame: This rectangle defines the size and position of the view in its superview’s coordinate system. Use this rectangle during layout operations to set the size and position the view. Setting this property changes the point specified by the center property and changes the size in the bounds rectangle accordingly. The coordinates of the frame rectangle are always specified in points.

    frame:一个矩形框,描述了view在其父控件坐标系中的大小和位置。修改frame会相应的改变center的值以及bounds.size属性。

    bounds:The bounds rectangle, which describes the view’s location and size in its own coordinate system.The default bounds origin is (0,0) and the size is the same as the size of the rectangle in the frame property. Changing the size portion of this rectangle grows or shrinks the view relative to its center point. Changing the size also changes the size of the rectangle in the frame property to match. The coordinates of the bounds rectangle are always specified in points.

    bounds:一个矩形框,描述了view在其自己坐标系中的大小和位置。bounds.origin默认是(0,0),bounds.size和frame.size一致。改变bounds.size,会相对view的 center point增加或减少矩形框。改变bounds.size也相应的改变 frame.size。

    --》这里我补一句,既然相对于center point向四周增加、减少,那必然也会改变frame.origin

    --》对比还发现,更改bounds并没有改变center property,但是修改frame会改变,也就引发了后文提及的注意事项。

    总结:

    1. 改变bounds的origin,相当于改变view内容显示的起点位置。比如,红色矩形view在右下角有个按钮(该按钮为红色view的子控件),如果origin设置为(20,20),那么会发现按钮开始靠近左上角,发生了位置偏移。

    2. 修改一个控件的bounds.origin,不会改变该控件以及其子控件的frame,但是由于显示内容的起点位置发生改变,因此,会相对(相对于控件的左上角)改变子控件的显示位置。因此,其子控件的frame中的origin,并不一定是该控件的左上角,只有当该控件的bounds的origin为(0,0)时才是左上角。

    3. 其实,控件的内容是无限大的,只不过我们在这个平面建立了一个坐标系,确定了坐标原点,确定了显示的起点位置而已。就好比你拿着望远镜(view)在看风景(view的内容),改变的只是你看到的区域(view.bounds),风景的相对位置并没有改变(子控件的frame不会改变),但是给你的感觉是风景在移动(相对改变子控件的显示位置)。

    4. 通过修改一个控件的frame.size来修改控件的长宽,长宽向右下增加/减少(不会改变该控件frame.origin)。但是通过修改bounds.size修改控件的长宽,长宽是向四周均匀增加/减少(会改变该控件的frame.origin)。

    注意:

    1. 在开发中设置一个控件的center时,最好先设置控件的宽高(size.width & size.height),再设置其center。

    2. 如果使用frame.size来设置width & height应该先设置width & height再设置center。

     如果使用bounds.size 来设置width & height 设置顺序不影响结果。

    3. 设置center时,不管哪种情况,先大小 后位置 不容易出问题。

    实例:

    理解bounds之后,我们可以模仿UIScrollView,让控制器的View也可以实现滚动功能,并限制其滚动范围。

    - (void)viewDidLoad {
        [super viewDidLoad];
        // 添加拖拽手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(offset:)];
        [self.view addGestureRecognizer:pan];
    }
    
    - (void)offset:(UIPanGestureRecognizer *)pan {
        CGPoint offset = [pan translationInView:self.view];
        CGRect temp = self.view.bounds;
        temp.origin.x += - offset.x;
        temp.origin.y += - offset.y;
        if (temp.origin.y < -100 || temp.origin.y > 0 || temp.origin.x < -150 || temp.origin.x > 0) {
            temp.origin.x = self.view.bounds.origin.x;
            temp.origin.y = self.view.bounds.origin.y;
        }
        self.view.bounds = temp;
        // 复位
        [pan setTranslation:CGPointZero inView:self.view];
        NSLog(@"%f",offset.y);
    }

    相关链接:关于UIScollView 中的contentOffset 的理解


    效果

      

  • 相关阅读:
    Nginx进阶-不停服更新
    Tengine安装
    初识Nginx
    自建Nuget服务器
    WebApi PUT、DELETE请求时出现405
    Mysql分页查询性能分析
    Asp.Net WebApi Swagger终极搭建
    到底什么是程序员思维,设计师思维
    [有了SSL证书,如何在IIS环境下部署https?【转载】]
    div 文章内容自动分屏显示
  • 原文地址:https://www.cnblogs.com/chao8888/p/5335360.html
Copyright © 2020-2023  润新知