Why iOS coordinate system is difficult to understand?? only me?

筅森魡賤 提交于 2019-12-19 11:52:27

问题


I'm studying iOS' UIView! And I found that I can't understand how bounds works.

For example,

Please run this code ... and see red box's moving. The red box goes up! and white root view is static!

Why!? why red box goes up! ?? please let me know 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.
    }

}

I read this article, but I can not understand

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

HERE! this is right ( according to what I tested, it was right. ) but, I don't know why. Why not "View.frame.origin.x + Superview.bounds.origin.x" ?

If we change the origin of view's bounds and the view moves on screen , what is the difference between "bounds" and "frame" in perspective of origin(or position) ? Is there a difference other than simply acting in reverse?


回答1:


Basically, The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

Please follow this link for clear example. Cocoa: What's the difference between the frame and the bounds?



来源:https://stackoverflow.com/questions/44922641/why-ios-coordinate-system-is-difficult-to-understand-only-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!