【问题标题】:Remove ScrollView Gradient Fade on bottom when reached bottom到达底部时移除底部的 ScrollView Gradient Fade
【发布时间】:2019-01-07 11:56:38
【问题描述】:

我有一个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
)
}

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

【问题讨论】:

    标签: ios swift uiscrollview cagradientlayer


    【解决方案1】:

    试试这个

    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 网址:https://github.com/sateeshyegireddi/GradientScrollViewDemo

    【讨论】:

    • 我有上面教程的完整源代码。如果你想要,我可以发给你,@Emre Önder。
    • 你能分享一下 updateGradientFrame() 吗?因为一切都在进行 updateGradientFrame()
    • 我之前从不同的帖子中看到了这个答案,但这会导致一个奇怪的错误。滚动后文本稍后出现
    • kvkkTextView是UILabel还是UITextView?应该是 UILabel。
    • 它是一个 UITextView。如何使用 UILabel 滚动?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多