代码之家  ›  专栏  ›  技术社区  ›  Emre Önder

当到达底部时移除底部的滚动视图渐变淡入度

  •  1
  • Emre Önder  · 技术社区  · 6 年前

    我有一个 UIScrollView 它有一个淡出的底部渐变层(就像在屏幕截图下面)。我想把它拿下来 UISCLVIEW . 我在下面找到了一些帖子,但没有什么对我有用的。

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

    它会引起一个奇怪的虫子。当我滚动它时,屏幕上看到的文本会变慢(当我快速滚动它时,屏幕上会出现空白,然后文本会出现)。

    screenshot

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sateesh Yemireddi    6 年前

    试试这个

    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