Fade bottom of UIScrollView to transparent

后端 未结 4 1767
囚心锁ツ
囚心锁ツ 2020-12-18 05:22

I have a UIScrollView, and I need the bottom to fade to transparent, so that it does not abruptly cut off the content. The background of the UIScrollView is a custom color.

4条回答
  •  悲&欢浪女
    2020-12-18 05:35

    Really like Steph Sharp's implementation, I converted it to swift and thought I'd share in case anyone else needs it.

    let fadePercentage = CGFloat(0.2)
    
    override func layoutSubviews() {
    
        super.layoutSubviews()
    
        var transparent = UIColor.clearColor().CGColor
        var opaque = UIColor.blackColor().CGColor
    
        var maskLayer = CALayer()
        maskLayer.frame = self.bounds
    
        var gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRectMake(self.bounds.origin.x, 0, self.bounds.size.width, self.bounds.size.height)
        gradientLayer.colors = [transparent, opaque, opaque, transparent]
        gradientLayer.locations = [0, fadePercentage, 1 - fadePercentage, 1]
    
        maskLayer.addSublayer(gradientLayer)
        self.layer.mask = maskLayer
    
    }
    

提交回复
热议问题