我正在try 转换一些视点中心,以便能够在这CGPoint
之间划清界限.
然而,这些点完全垂直对齐,convert
方法返回意外的值.显然,我确实遗漏了一些东西,也搞不清楚.我在此请求:)
我快速设置了这个代码,重现了这个问题:
class ViewController: UIViewController {
private lazy var backgroundView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemBackground
return view
}()
private lazy var firstView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemRed
return view
}()
private lazy var secondView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemBlue
return view
}()
private lazy var firstSubview: UIImageView = {
let view = UIImageView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemMint
return view
}()
private lazy var secondSubview: UIImageView = {
let view = UIImageView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .systemGreen
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(backgroundView)
backgroundView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
backgroundView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
backgroundView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
backgroundView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
backgroundView.addSubview(firstView)
backgroundView.addSubview(secondView)
firstView.topAnchor.constraint(equalTo: backgroundView.topAnchor).isActive = true
firstView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
firstView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
secondView.topAnchor.constraint(equalTo: firstView.bottomAnchor, constant: 10).isActive = true
secondView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
secondView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
firstView.addSubview(firstSubview)
firstSubview.topAnchor.constraint(equalTo: firstView.topAnchor).isActive = true
firstSubview.bottomAnchor.constraint(equalTo: firstView.bottomAnchor).isActive = true
firstSubview.leadingAnchor.constraint(equalTo: firstView.leadingAnchor).isActive = true
firstSubview.heightAnchor.constraint(equalToConstant: 50).isActive = true
firstSubview.widthAnchor.constraint(equalToConstant: 50).isActive = true
secondView.addSubview(secondSubview)
secondSubview.topAnchor.constraint(equalTo: secondView.topAnchor).isActive = true
secondSubview.bottomAnchor.constraint(equalTo: secondView.bottomAnchor).isActive = true
secondSubview.leadingAnchor.constraint(equalTo: secondView.leadingAnchor, constant: 10).isActive = true
secondSubview.heightAnchor.constraint(equalToConstant: 30).isActive = true
secondSubview.widthAnchor.constraint(equalToConstant: 30).isActive = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let firstCenter1 = firstSubview.convert(firstSubview.center, to: view)
let secondCenter1 = secondSubview.convert(secondSubview.center, to: view)
print(firstCenter1, secondCenter1)
}
}
打印结果是(45.0, 84.0) (55.0, 134.0)
,然而,正如我预期的那样,我应该打印出(45.0, 84.0) (45.0, 134.0)
.因为secondSubview
的亲属center.x
就是字面上的45.0
和firstSubview
.
我混合并匹配了S的参数和接球手,但没有运气.即使我得到center.x
,center.y
也给出了错误的值.
我错过了什么吗?