Gradient at the top / Bot of Scrollview with additional content

I have a scrollview> ContentView> TextLabel function where the gradient is displayed but does not work as I want it. This is a clean scrollView background, so all I'm looking for is a transparent gradient mask above the text. I found something similar to what I am looking for in Objective-C, but this thread does not have a Swift solution.

My ultimate goal seems to be something that most people can use, so I'm surprised there is no Swift version yet. The function I'm trying to execute is:

  • Sometimes the text fits perfectly with the fixed size of scrollView, so there should be no gradient.
  • When the text is longer than it can fit, and therefore some of them are below the lower scroll compartment, the bottom of the view must disappear to clear.
  • As soon as the user scrolls down and the top should disappear, indicating that there is more.

I tried this code to handle bullet # 2:

        let gradient = CAGradientLayer()
        gradient.frame = self.bio_ScrollView.superview!.bounds ?? CGRectNull
        gradient.colors = [UIColor.whiteColor().CGColor, UIColor.clearColor().CGColor]
        //gradient.locations = [0, 0.15, 0.25, 0.75, 0.85, 1.0]
        gradient.locations = [0.6, 1.0]
        self.bio_ScrollView.superview!.layer.mask = gradient

But it all disappears, including the button at the bottom, which is clearly not in scrollview:

scrollView with gradient

If I remove .superview and apply it directly to scrollView, it just fades out all the text below the initial part that was visible:

scrollview gradient not working

Does anyone know anything about how to implement this correctly?

+4
source share
1 answer

. -, , . ( ):

Gradient View Hierarchy

  • / : ,
  • Scrollview: (4 )
  • : + X (5 )
  • : (4 )

"scrollViewDelegate" ViewController:

class ViewController_WithScrollView: UIViewController, UIScrollViewDelegate {
   ....

, , IBOutlets. scrollView viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    yourScrollView.delegate = self
    //+All your other viewDidLoad stuff
}

scrollViewDidScroll, , .

func scrollViewDidScroll (scrollView: UIScrollView) {
    if scrollView.isAtTop {
        self.applyGradient_To("Bot")
    } else if scrollView.isAtBottom {
        self.applyGradient_To("Top")
    } else {
        self.applyGradient_To("Both")
    }
} 

:

func applyGradient_To (state: String) {
    let gradient = CAGradientLayer()
    gradient.frame = self.yourScrollView.superview!.bounds ?? CGRectNull

    switch state {
    case "Top":
        gradient.colors = [UIColor.clearColor().CGColor,UIColor.whiteColor().CGColor]
        gradient.locations = [0.0,0.2]
    case "Bot":
        gradient.colors = [UIColor.whiteColor().CGColor, UIColor.clearColor().CGColor]
        gradient.locations = [0.8,1.0]
    default:
        gradient.colors = [UIColor.clearColor().CGColor,UIColor.whiteColor().CGColor,UIColor.whiteColor().CGColor, UIColor.clearColor().CGColor]
        gradient.locations = [0.0,0.2,0.8,1.0]
    }
    self.yourScrollView.superview!.layer.mask = nil
    self.yourScrollView.superview!.layer.mask = gradient
}

!

+1

All Articles