Skip to content

Instantly share code, notes, and snippets.

@cyrilchandelier
Created August 21, 2019 01:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrilchandelier/227cdef219c509a2550fd1590edd6d8e to your computer and use it in GitHub Desktop.
Save cyrilchandelier/227cdef219c509a2550fd1590edd6d8e to your computer and use it in GitHub Desktop.
An extension to detect if scrollview is bouncing
extension UIScrollView {
var isBouncing: Bool {
return isBouncingTop || isBouncingBottom
}
var isBouncingTop: Bool {
return contentOffset.y < topInsetForBouncing - contentInset.top
}
var isBouncingBottom: Bool {
let threshold: CGFloat
if contentSize.height > frame.size.height {
threshold = (contentSize.height - frame.size.height + contentInset.bottom + bottomInsetForBouncing)
} else {
threshold = topInsetForBouncing
}
return contentOffset.y > threshold
}
private var topInsetForBouncing: CGFloat {
return safeAreaInsets.top != 0.0 ? -safeAreaInsets.top : 0.0
}
private var bottomInsetForBouncing: CGFloat {
return safeAreaInsets.bottom
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment