到达底部时删除底部的 ScrollView Gradient Fade

Remove ScrollView Gradient Fade on bottom when reached bottom

我有一个 UIScrollView,它有一个淡出的底部渐变层(如下面的屏幕截图所示)。我想在到达 UIScrollView 的底部时将其删除。我找到了以下帖子,但对我没有任何帮助。

https://stablekernel.com/how-to-fade-out-content-using-gradients-in-ios/

Fade edges of UITableView

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
}

当我尝试在

中更改渐变框架时
private func updateGradientFrame() {
gradient.frame = CGRect(
    x: 0,
    y: kvkkTextView.contentOffset.y,
    width: kvkkTextView.bounds.width,
    height: kvkkTextView.bounds.height
)
}

它会导致一个奇怪的错误。当我滚动它时,屏幕上看到的文字很慢(当我快速滚动它时,屏幕看到空白然后出现文字)。

试试这个

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
    }
}

更新:

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