[英]How to get the distance between two anchors?
我知道如何快速獲得兩點之間的距離 。 但是我想知道如何以CGFloat的方式獲得兩個錨點之間的距離。
例如:我要查找之間的距離
view.topAnchor
和
button.topAnchor
我猜我將不得不獲得錨點的CGPoint(然后我可以找到CGPoint的y點之間的差)。 我只是不知道該怎么做。
錨實際上是在構造約束。 如果您已經在使用它們,則只需使用constraint.constant屬性即可獲取值。 像這樣,
let view = UIView()
let button = UIButton()
view.addSubview(button)
let heightConstraint = button.topAnchor.constraint(equalTo: view.topAnchor)
heightConstraint.isActive = true
view.layoutIfNeeded() // update incase still not updated
print(heightConstraint.constant)
但是我認為您真正想要實現的是
let distance = button.frame.minY - view.frame.minY
這樣測量距離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.