为什么iOS坐标系很难理解?只有我? [英] Why iOS coordinate system is difficult to understand?? only me?

查看:91
本文介绍了为什么iOS坐标系很难理解?只有我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究iOS的UIView!
而且我发现我不明白界限的工作原理。



例如,



请运行此代码...,然后查看红色框的移动情况。
红色框上升!



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

  class ViewController:UIViewController {

覆盖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)

}


覆盖func viewDidAppear(_animated:Bool){

UIView.animate(withDuration:8,动画:{

//我无法理解边界的工作方式!
self.view.bounds = CGRect(x:0,y:-300, width:self.view.bounds.width,height:700)

//我能理解框架的工作原理
self.view.frame = CGRect(x:200,y:0,宽度:self.view.bounds.width,高度:self.view.frame.height)
})

}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何r可以重新创建的资源。
}

}

我读了这篇文章,但我听不懂

  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?



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

解决方案

基本上,UIView的边界是矩形,表示为位置(x,y)和大小(宽度,



UIView的框架是矩形,表示为位置(x,y)和大小(

请点击此链接以获取清晰的示例。 可可:框架和边界之间有什么区别?


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?

解决方案

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?

这篇关于为什么iOS坐标系很难理解?只有我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆