【问题标题】:How iOS's coordinate system works? [duplicate]iOS 的坐标系是如何工作的? [复制]
【发布时间】:2017-07-06 00:35:40
【问题描述】:

很高兴认识你。我正在研究 iOS 的 UIView 的“坐标系”。 Frame 很容易理解,但 Bounds 不是。

例如,当您更改原点时,Frame 会按预期工作。

即使我们改变原点,Bounds 也不会改变它的位置

import UIKit

class ViewController: UIViewController {

    var childView : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let rect = CGRect(x: 20, y: 30, width: 200, height: 200)
        childView = UIView(frame: rect)
        childView.backgroundColor = UIColor.red
        self.view.addSubview(childView)
    }

    override func viewDidAppear(_ animated: Bool) {
        UIView.animate(withDuration: 5, animations: {
            self.childView.bounds = CGRect(x: 100, y: 150, width: 200, height: 200)
        })
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

“为什么我们需要 Bounds?”我想。 我在互联网上努力搜索。所以,我发现了一些事实。

----首先----

Bounds 字面意思是边界线。 iOS 只能在该边界线内绘制图片。上图可以看到,UIButton也是有Bounds的,超过Bounds的时候,图片就被剪切绘制了。

---- 第二个---- 边界大小和框架大小相同。

这不重要,但我试着测试一下。

---- 三分之二 ---- bounds.origin 在 View Hierarchy 中也很有用。但它的工作方式与 Frame 不同。

框架很容易理解。 ViewController 的 RootView 将从顶部到按钮为 700。

import UIKit

class ViewController: UIViewController {

    var childView : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let rect = CGRect(x: 100, y: 100, width: 200, height: 200)
        childView = UIView(frame: rect)
        childView.backgroundColor = UIColor.red
        self.view.addSubview(childView)
        self.view.backgroundColor = UIColor.cyan
    }

    override func viewDidAppear(_ animated: Bool) {
        UIView.animate(withDuration: 10, animations: {

        self.view.frame = CGRect(x: 0, y: 700, width: self.view.frame.size.width, height: self.view.frame.size.height)

        })
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

如果您查看 iOS 中的坐标系,您会发现 Frame 应该是这样工作的。

然而,Bounds 不会移动自己,而只会移动它下面的Subview。我不明白这部分。

import UIKit

class ViewController: UIViewController {

    var childView : UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let rect = CGRect(x: 100, y: 100, width: 200, height: 200)
        childView = UIView(frame: rect)
        childView.backgroundColor = UIColor.red
        self.view.addSubview(childView)
        self.view.backgroundColor = UIColor.cyan
    }

    override func viewDidAppear(_ animated: Bool) {
        UIView.animate(withDuration: 10, animations: {

        self.view.bounds = CGRect(x: 0, y: 700, width: self.view.frame.size.width, height: self.view.frame.size.height)

        })
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

所以,我在谷歌搜索它。 最终,我找到了一个数学公式,用于在 SuperView.origin.bounds 更改时重新定位“SubView”。 ( 출처 : article )

CompositedPosition.x = View.frame.origin.x - Superview.bounds.origin.x;
CompositedPosition.y = View.frame.origin.y - Superview.bounds.origin.y;

顺便问一下,Apple 为什么使用这些公式?基本上,基于我们认为的“坐标系”,

CompositedPosition.x = View.frame.origin.x + Superview.bounds.origin.x;
CompositedPosition.y = View.frame.origin.y + Superview.bounds.origin.y;

这个公式不是更直观吗?

所以,我的问题是这些。

  1. 当我在原始状态下将 SuperView.bounds.origin.y 增加 700 时,为什么 SubView 向上移动而不是向下移动???
  2. 我应该接受公式并记住它吗?

------------------------------------------ -------------------

--------------编辑--1------------ ----------------

现在我通过学习 UIScrollView 了解了边界的概念!

CompositedPosition.x = View.frame.origin.x - Superview.bounds.origin.x;
CompositedPosition.y = View.frame.origin.y - Superview.bounds.origin.y;

这是对的。

这是我理解的图片。

是的,我同意这张照片看起来头晕抱歉!

如果我滚动 UP 滚动视图,bounds.origin.y 将增加,offset.y 将增加,并且附加在滚动视图的 frame.origin.y 中的子视图将 改变但是,iOS 使用该公式计算子视图的绘制位置(CompositedPosition)!看起来子视图会UP!。

简而言之,边界变化 -> iOS 使用该公式计算 -> 绘制!

【问题讨论】:

  • 1.使用英语。 2. 转到 1 :)
  • 这是一个英文网站,你的帖子需要用英文写。图像也太多了。代码必须是文本,格式正确,可读。如果您觉得需要对其进行注释,请在代码中使用 cmets。这篇文章对代理背后的人或在移动设备上阅读的人没有任何用处,带宽有限(有时是昂贵)的人会说你非常粗鲁的话。
  • 对不起!我现在翻译。
  • 感谢您的链接!

标签: ios uiview frame bounds


【解决方案1】:

首先,请用英文或其他支持的语言发帖。

其次,我想我可能已经理解您的目的了,这是一般简单的答案。

首先,所有事物都有一个坐标系,但每个视图也有自己的坐标系。视图的每个子视图都在父视图坐标系内定义。

我会尝试说更多,但我纯粹是为了照片。而且即使帖子是英文的,我也很难相信这篇文章是所提出问题的最小示例

【讨论】:

  • 感谢您的帖子!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 2019-01-23
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多