【发布时间】: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