forked from robb/Cartography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewUtils.swift
46 lines (36 loc) · 925 Bytes
/
ViewUtils.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// ViewUtils.swift
// Cartography
//
// Created by Garth Snyder on 11/23/14.
// Copyright (c) 2014 Robert Böhnke. All rights reserved.
//
#if os(iOS)
import UIKit
#else
import AppKit
#endif
internal func closestCommonAncestor(a: View, b: View) -> View? {
let (aSuper, bSuper) = (a.superview, b.superview)
if a === b { return a }
if a === bSuper { return a }
if b === aSuper { return b }
if aSuper === bSuper { return aSuper }
let ancestorsOfA = Set(ancestors(a))
for ancestor in ancestors(b) {
if ancestorsOfA.contains(ancestor) {
return ancestor
}
}
return .None
}
private func ancestors(v: View) -> AnySequence<View> {
return AnySequence { () -> AnyGenerator<View> in
var view: View? = v
return anyGenerator {
let current = view
view = view?.superview
return current
}
}
}