Fade out scrolling UITextView over image?

不问归期 提交于 2019-11-27 19:47:26

Oh man, I use this a lot. Have it saved as a snippet:

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.frame = self.textView.superview.bounds;
gradient.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor];
gradient.locations = @[@0.0, @0.03, @0.97, @1.0];

self.textView.superview.layer.mask = gradient;

This solution requires that your text view be embedded in a view of its own. This applies a 3% fade to the top and bottom of the text view. Modify gradient.locations to your needs.

I've been inspired by others from this thread, but have converted the code to Swift and used start and end point instead, to make a gradient at the bottom.

if let containerView = textView.superview {
    let gradient = CAGradientLayer(layer: containerView.layer)
    gradient.frame = containerView.bounds
    gradient.colors = [UIColor.clearColor().CGColor, UIColor.blueColor().CGColor]
    gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
    gradient.endPoint = CGPoint(x: 0.0, y: 0.85)
    containerView.layer.mask = gradient
    }

See: image of textView bottom with gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!