ios - 为什么iOS坐标系很难理解??只有我?

标签 ios uiview frame bounds coordinate

我正在研究iOS的UIView! 我发现我无法理解边界是如何工作的。

例如,my code

请运行此代码...并看到红色框在移动。 红框升起来了!白色 Root View 是静态的!

为什么!?为什么红框会上升! ??请让我知道OTL!

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let childView : UIView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 200) )
        childView.backgroundColor = UIColor.red

        self.view.addSubview(childView)

    }


    override func viewDidAppear(_ animated: Bool) {

        UIView.animate(withDuration: 8, animations: {

          // I cannot unnerstand how bounds works!!
          self.view.bounds = CGRect(x: 0, y: -300, width:self.view.bounds.width, height: 700)

          //I can understand how frame works
          self.view.frame = CGRect(x: 200, y: 0, width: self.view.bounds.width, height: self.view.frame.height)
      })

    }

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

}

我读了this article ,但我无法理解

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

这里!这是正确的(根据我的测试,这是正确的。)但是,我不知道为什么。为什么不是“View.frame.origin.x + Superview.bounds.origin.x”?

如果我们改变 View 边界的原点并且 View 在屏幕上移动,那么从原点(或位置)的角度来看,“边界”和“框架”有什么区别?除了简单地反向操作之外还有什么区别吗?

最佳答案

基本上,UIView 的边界是矩形,表示为相对于其自身坐标系 (0,0) 的位置 (x,y) 和大小(宽度,高度)。

UIView 的框架是一个矩形,表示为相对于它所包含的父 View 的位置(x,y)和大小(宽度,高度)。

请点击此链接以获取清晰的示例。 Cocoa: What's the difference between the frame and the bounds?

关于ios - 为什么iOS坐标系很难理解??只有我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44922641/

相关文章:

ios - 使用静态库将 AFNetworking 添加到项目我们 AFNetworking

iOS - 以编程方式切换 WiFi 网络 - 越狱

ios - 使用 Firebase 通知定位单个设备 (swift)

ios - UIView 以编程方式完成触摸

javascript - HTML OnClick() 事件不起作用

python - Tkinter:在框架类中创建多个框架

swift - 在 tvOS 12 的 TVUIKit 中使用 TVPosterImage

ios - UIScrollView 未显示在 View 中

ios - 计算一个 View Controller 和另一个 View Controller 的切换之间的时间

java - 如何使用 Infonode 库将生成的动态 View 添加到根窗口?