Skip to content

Instantly share code, notes, and snippets.

@DianQK
Last active February 10, 2016 17:05
Show Gist options
  • Save DianQK/df41ba56ceb35e0242b8 to your computer and use it in GitHub Desktop.
Save DianQK/df41ba56ceb35e0242b8 to your computer and use it in GitHub Desktop.
import UIKit
func recurisiveSearchParentViewUntil<T: UIView>(type: T.Type, currentView: UIView) -> T? {
switch currentView {
case let view as T:
return view // 当前 view 满足则返回 view
case let view where view.superview != nil:
return recurisiveSearchParentViewUntil(type, currentView: view.superview!)
default:
return nil
}
}
@DianQK
Copy link
Author

DianQK commented Feb 10, 2016

func recurisiveSearchParentViewUntil<T: UIView>(type: T.Type, currentView: UIView?) -> T? {
    guard let currentView = currentView else { return nil }
    return (currentView as? T) ?? recurisiveSearchParentViewUntil(type, currentView: currentView.superview)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment