Skip to content

Instantly share code, notes, and snippets.

@boska
Last active April 30, 2019 12:56
Show Gist options
  • Save boska/f9b882dc399c046caefc5e4f783d4687 to your computer and use it in GitHub Desktop.
Save boska/f9b882dc399c046caefc5e4f783d4687 to your computer and use it in GitHub Desktop.
Objective-C to Swift - FLEX
// Rewrite from FLEX
import RxCocoa
import RxGesture
import RxSwift
// https://github.com/Flipboard/FLEX/blob/master/Classes/ExplorerInterface/FLEXExplorerViewController.m#L611
var overlay: UIWindow?
func findViews(at point: CGPoint, ignoreHidden: Bool = true) -> [UIView] {
return UIApplication.shared.windows
.filter { $0 != overlay && $0.point(inside: point, with: nil) }
.flatMap {
[$0] + $0.recursiveSubviews(at: point, ignoreHidden: ignoreHidden)
}
}
extension UIView {
func recursiveSubviews(at point: CGPoint, ignoreHidden: Bool = true) -> [UIView] {
return subviews.filter {
// filter out non-visible view if ignoreHidden == true
ignoreHidden && !$0.isHidden && $0.alpha > 0.01
}.flatMap { view -> [UIView] in
if view.frame.contains(point) {
// subview contains point
return [view] + view.recursiveSubviews(at: convert(point, to: view))
} else if !view.clipsToBounds {
// no contain but subview clipsToBounds == false keep looking subviews
return view.recursiveSubviews(at: convert(point, to: view))
} else {
// nothing happens
return []
}
}
}
}
class Explorer: UIViewController {
let bag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(white: 0, alpha: 0.2)
view.rx.tapGesture()
.when(.recognized)
.asLocation(in: .view)
.debug()
.map {
findViews(at: $0).last!
}
.debug()
.subscribe()
.disposed(by: bag)
view.rx
.swipeGesture(.down)
.when(.recognized)
.subscribe(onNext: { _ in
overlay?.isHidden = true
}).disposed(by: bag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment