From 27012934323e3e3202c405c9b40d2400bf65447f Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Tue, 17 Feb 2015 11:37:43 +1300 Subject: [PATCH] Improve the closest superview finder algorithm --- Snap/Constraint.swift | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Snap/Constraint.swift b/Snap/Constraint.swift index 7052ab3..2465af8 100644 --- a/Snap/Constraint.swift +++ b/Snap/Constraint.swift @@ -506,19 +506,27 @@ public class Constraint { } private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? { - var closestCommonSuperview: View? - var secondViewSuperview: View? = toView - while closestCommonSuperview == nil && secondViewSuperview != nil { - var firstViewSuperview = fromView - while closestCommonSuperview == nil && firstViewSuperview != nil { - if secondViewSuperview == firstViewSuperview { - closestCommonSuperview = secondViewSuperview + var views = NSMutableSet() + var fromView = fromView + var toView = toView + do { + if let view = toView { + if views.containsObject(view) { + return view } - firstViewSuperview = firstViewSuperview?.superview + views.addObject(view) + toView = view.superview } - secondViewSuperview = secondViewSuperview?.superview - } - return closestCommonSuperview + if let view = fromView { + if views.containsObject(view) { + return view + } + views.addObject(view) + fromView = view.superview + } + } while (fromView != nil || toView != nil) + + return nil } }