Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@jasl

This comment has been minimized.

Copy link

@jasl jasl commented Feb 10, 2016

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

    if let view = currentView as? T {
        return view
    }

    return recurisiveSearchParentViewUntil(type, currentView: currentView.superview)
}
@DianQK

This comment has been minimized.

Copy link
Owner Author

@DianQK 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