0

I have a bottom fade-out gradient layer on UITextView. I'm deleting this fade-out when user reaches the bottom of UIScrollView. However, when user started to scroll to top, I set it again.

My Problem is that I want to delete this CAGradientLayer with alpha animation not just directly remove it.

Now I'm removing and setting it with (without animation):

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    updateGradientFrame()
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
           self.myTextView.layer.mask = nil
    }else{
        myTextView.layer.mask = gradient
    }}

What I tried and doesn't work:

UIView.animate(withDuration: 1) {
   self.myTextView.layer.mask = nil
}

Another try (this adds animation to whole UITextView not just the layer):

let flash = CABasicAnimation(keyPath: "opacity")
flash.fromValue = 1.0
flash.toValue = 0.0
flash.duration = 3.0 // 1 second
flash.autoreverses = false // Back

gradient.add(flash, forKey: "flashAnimation")

What I found on community (Not directly refers to this question):

Animating a CALayer's mask size change

Animate CALayer mask change with fade effect

Emre Önder
  • 2,408
  • 2
  • 23
  • 73

1 Answers1

0

What about something like this ?

UIView.animate(withDuration: 1, animations: {
        yourLayer.opacity = 0
    }, completion: {
        yourLayer.removeFromSuperLayer()
    })

Here you animate the opacity with 1 as duration, then when it completes you just remove it from its super layer.

TibiaZ
  • 730
  • 1
  • 8
  • 21