Last active
November 6, 2020 11:56
-
-
Save chriswebb09/1e4e5a1aa5f2e6fa3896c0a28975c5e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class ViewController: UIViewController { | |
| let viewOne = UIView() | |
| let viewTwo = UIView() | |
| let intermediaryView = UIView() | |
| let viewSuper = UIView() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| viewOne.tag = 22 | |
| viewTwo.tag = 23 | |
| viewSuper.tag = 10 | |
| view.tag = 12 | |
| intermediaryView.addSubview(viewOne) | |
| intermediaryView.tag = 8 | |
| view.addSubview(viewTwo) | |
| view.addSubview(intermediaryView) | |
| let traverser = ViewTraverser() | |
| let nameView = traverser.commonSuper(viewOne: viewOne, viewTwo: viewTwo) | |
| dump(nameView) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewTraverser { | |
| func commonSuper(viewOne: UIView, viewTwo: UIView) -> UIView? { | |
| var inputTwo: UIView? = viewTwo | |
| let superViews: [Int: UIView] = traverseSuperViews(view: viewOne) | |
| while inputTwo != nil { | |
| if checkForSuper(view: inputTwo, views: superViews) != nil { | |
| return inputTwo | |
| } else { | |
| inputTwo = inputTwo?.superview | |
| } | |
| } | |
| return nil | |
| } | |
| func traverseSuperViews(view: UIView) -> [Int : UIView] { | |
| var views: [Int: UIView] = [:] | |
| var inputView: UIView? = view | |
| while inputView != nil { | |
| guard let tag = inputView?.tag, let view = inputView else { continue } | |
| views[tag] = view | |
| inputView = view.superview | |
| } | |
| return views | |
| } | |
| func checkForSuper(view: UIView?, views: [Int: UIView]) -> UIView? { | |
| guard let view = view else { return nil } | |
| if views[view.tag] != nil { | |
| return view | |
| } | |
| return nil | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment