我正在尝试查找最近的可能子视图。在我的应用程序中,有7个UIButton
s和一个UILabel
s居中以下20的余量。它不是子视图。在Swift中找到最近的UIView的最佳方法
我想要什么?用较小的努力找出离按钮最近的标签。
我现在在做什么?我在函数中传递UIButton并计算从该按钮到所有标签的可能距离。然后对距离进行排序会导致上升并选取第一个对象以获得最小距离,这应该是我最近的标签。
internal func findNearByLabel(withRespectToButton button: UIButton) -> UILabel? {
var distance = Array<CGFloat>()
var labels: Array<UILabel> = self.view.subViews(type: UILabel.self)
labels.forEach { (label) in
distance.append(self.distance(label.frame.origin, button.frame.origin))
}
if !distance.isEmpty {
let sortedDistance = Array.init(distance).sorted()
let minDistance = sortedDistance.first!
let indexOfMinDistance = distance.index(of: minDistance)!
return labels[indexOfMinDistance]
}
return nil
}
internal func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDistance = a.x - b.x
let yDistance = a.y - b.y
return CGFloat(sqrt((xDistance * xDistance) + (yDistance * yDistance)))
}
extension UIView {
func subViews<T : UIView>(type : T.Type) -> [T]{
var all = [T]()
for view in self.subviews {
if let aView = view as? T{
all.append(aView)
}
}
return all
}
}
什么是问题?没问题,它的工作原理,但我想检查这是最好的解决方案还是可以改进的?
同意@Sulthan,其实我只是用'flatMap'和'filter'思维https://codereview.stackexchange.com/ ... –
我大概可以减少一半的代码。您应该更喜欢'[CGFloat]'在'Array'上,并且尽可能使用'let'而不是'var'。距离可以使用'CGPoint.distance(to:)'上的扩展来定义,它应该可以使用'hypot'函数。 –
Sulthan