Advertisement
HXXXXJ

Find Clone View

Mar 25th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.16 KB | None | 0 0
  1. /*
  2.  
  3.  
  4.  You have a simple tree structure Ⓐ and its clone ⓐ.
  5.  
  6.  Each node in the tree has a pointer to it's parent as well as an array of its children.
  7.  
  8.  Given an original tree's node Ⓑ and cloned tree ⓐ, implement a method that returns ⓑ (the clone of Ⓑ).
  9.  (Imagine finding the matching UIButton/UISlider/UIView in a separate cloned view controller.)
  10.  
  11.  Original
  12.  Ⓐ
  13.  ┏━┻━━┓
  14.  ◯ ◯
  15.  ┏┻┓ ┏━╋━┓
  16.  ◯ ◯ ◯ ◯ ◯
  17.  ┏┻┓ ┃
  18.  ◯ Ⓑ ◯
  19.  
  20.  Clone
  21.  ⓐ
  22.  ┏━┻━━┓
  23.  ◯ ◯
  24.  ┏┻┓ ┏━╋━┓
  25.  ◯ ◯ ◯ ◯ ◯
  26.  ┏┻┓ ┃
  27.  ◯ ⓑ ◯  */
  28.  
  29.  
  30.  
  31. func findClonevView(_ cloneView: UIView, _ root: UIView) -> UIView{
  32.     var runner = cloneView
  33.     var arr = [Int]()
  34.    
  35.     while runner.superView != nil {
  36.         let temp = runner
  37.         runner = runner.superView!
  38.         if let index = runner.subViews.firstIndex(where: { (item) -> Bool in
  39.             item === temp
  40.         }){
  41.             arr.append(index)
  42.         }
  43.     }
  44.     runner = root
  45.     while arr.count > 0 {
  46.         let curr = arr.removeLast()
  47.         runner = runner.subViews[curr]
  48.     }
  49.     return runner
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement