我正在研究iOS的UIView!
我发现我无法理解边界是如何工作的。
For example,
请运行此代码...并看到红色框在移动。
红框升起来了!白色的根视图是静态的!
为什么!?为什么红框会上升! ??请让我知道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 本文,但我无法理解
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”?
如果我们改变视图边界的原点并且视图在屏幕上移动,那么在原点(或位置)的角度上,“边界”和“框架”之间有什么区别?除了简单地反向操作之外还有什么区别吗?