mirror of https://github.com/SnapKit/SnapKit
Improve the closest superview finder algorithm
This commit is contained in:
parent
116002cde7
commit
2701293432
|
@ -506,19 +506,27 @@ public class Constraint {
|
||||||
}
|
}
|
||||||
|
|
||||||
private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
|
private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
|
||||||
var closestCommonSuperview: View?
|
var views = NSMutableSet()
|
||||||
var secondViewSuperview: View? = toView
|
var fromView = fromView
|
||||||
while closestCommonSuperview == nil && secondViewSuperview != nil {
|
var toView = toView
|
||||||
var firstViewSuperview = fromView
|
do {
|
||||||
while closestCommonSuperview == nil && firstViewSuperview != nil {
|
if let view = toView {
|
||||||
if secondViewSuperview == firstViewSuperview {
|
if views.containsObject(view) {
|
||||||
closestCommonSuperview = secondViewSuperview
|
return view
|
||||||
}
|
}
|
||||||
firstViewSuperview = firstViewSuperview?.superview
|
views.addObject(view)
|
||||||
|
toView = view.superview
|
||||||
}
|
}
|
||||||
secondViewSuperview = secondViewSuperview?.superview
|
if let view = fromView {
|
||||||
|
if views.containsObject(view) {
|
||||||
|
return view
|
||||||
}
|
}
|
||||||
return closestCommonSuperview
|
views.addObject(view)
|
||||||
|
fromView = view.superview
|
||||||
|
}
|
||||||
|
} while (fromView != nil || toView != nil)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue