I have an UIScrollView
which has a fade-out bottom gradient layer (like in below screeshot). I want to remove it when reached to bottom of UIScrollView
. I found below posts but nothing worked for me.
https://stablekernel.com/how-to-fade-out-content-using-gradients-in-ios/
let gradient = CAGradientLayer()
gradient.frame = myTextView.bounds
gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor, UIColor.black.cgColor, UIColor.clear.cgColor]
gradient.locations = [0, 0.0, 0.7, 1]
myTextView.layer.mask = gradient
override func layoutSubviews() {
gradient.frame = kvkkTextView.bounds
}
When I try to change the frame of gradient in
private func updateGradientFrame() {
gradient.frame = CGRect(
x: 0,
y: kvkkTextView.contentOffset.y,
width: kvkkTextView.bounds.width,
height: kvkkTextView.bounds.height
)
}
It causes a weird bug. When I scroll it, texts seen on screen slowly (When I fastly scroll it, screen seen blank and then text appears).
Try this
func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateGradientFrame()
//On the scroll view content is over means there is no content anymore then hide the mask
if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.bounds.height {
label.layer.mask = nil
} else {
//Else show the mask of UILabel
label.layer.mask = gradient
}
}
Update:
private func updateGradientFrame() {
gradient.frame = CGRect(
x: 0,
y: scrollView.contentOffset.y,
width: scrollView.bounds.width,
height: scrollView.bounds.height
)
}
GitHub Url: https://github.com/sateeshyegireddi/GradientScrollViewDemo