Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

ios - Fade out scrolling UITextView over image?

I’m looking to fade out a scrolling UITextView over a background image, something similar to the gradient at the top of this little example.

enter image description here

I’m trying to work out how I can achieve this using CAGradientLayer and masks. As the text in the view scrolls (vertically), I need the text to become transparent before it hits the frame of the view, to give the illusion that it’s fading out upward and downward.

Any tips on how I might achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...