Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kebbbnnn/afd41000a30c462380fda500485eb3b4 to your computer and use it in GitHub Desktop.
Save kebbbnnn/afd41000a30c462380fda500485eb3b4 to your computer and use it in GitHub Desktop.
[WIP] UIView to check is visible on screen. Not perfect solution
extension UIView {
func isPossiblyVisible() -> Bool {
guard isHidden == false,
alpha > 0,
bounds != .zero,
let window = window, // In a window's view hierarchy
window.isKeyWindow, // Does not consider cases covered by another transparent window
window.hitTest(convert(center, to: nil), with: nil) != self
else { return false }
// Checck subviews
let invisibleSubviews: Bool = {
for subview in subviews {
if subview.isPossiblyVisible() {
return false
}
}
return true // Including no subviews
}()
// No subview and no color, its not visible
if invisibleSubviews {
if (backgroundColor == nil || backgroundColor == .clear)
&& (layer.backgroundColor == nil || layer.backgroundColor?.alpha == 0){
return false
}
}
// What about special CALayer cases?
return true // maybe, not 100% sure XD
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment