【问题标题】:Why iOS coordinate system is difficult to understand?? only me?为什么iOS坐标系很难理解??只有我?
【发布时间】:2017-07-05 09:46:17
【问题描述】:

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

例如,

请运行这段代码……看看红框在移动。 红盒子升起来了!而白色的根视图是静态的!

为什么!?为什么红框会上升! ??请告诉我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" ?

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

【问题讨论】:

  • 我们无法运行屏幕截图;你应该粘贴代码。
  • 对不起,我现在添加代码。

标签: ios uiview frame bounds coordinate


【解决方案1】:

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

UIView 的框架是一个矩形,表示为相对于它所在的父视图的位置 (x,y) 和大小 (width,height)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2018-08-01
    • 2021-12-29
    • 1970-01-01
    • 2022-01-23
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多