我在学习iOS'UIView! 而且我发现我无法理解边界是如何工作的。
请运行此代码...并看到红色框移动。 红色的盒子上升了!和白根视图是静态的!
为什么!?为什么红盒子上升! ??请告诉我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”?
如果我们改变视图边界的原点并且视图在屏幕上移动,那么在原点(或位置)的角度来看,“边界”和“框架”之间有什么区别?除了简单地反向行动之外还有区别吗?
答案 0 :(得分:1)
基本上,UIView的边界是矩形,表示为相对于其自身坐标系(0,0)的位置(x,y)和大小(宽度,高度)。
UIView的框架是矩形,表示为相对于其所包含的超级视图的位置(x,y)和大小(宽度,高度)。
请点击此链接获取明确示例。 Cocoa: What's the difference between the frame and the bounds?