Created
August 21, 2019 01:42
-
-
Save cyrilchandelier/227cdef219c509a2550fd1590edd6d8e to your computer and use it in GitHub Desktop.
An extension to detect if scrollview is bouncing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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