ios - 在图像上淡出滚动 UITextView 吗?

标签 ios core-graphics cagradientlayer

我希望在背景图像上淡出滚动的UITextView,类似于这个小示例顶部的渐变。

enter image description here

我正在尝试弄清楚如何使用 CAGradientLayer 和蒙版来实现这一目标。当 View 中的文本滚动(垂直)时,我需要文本在到达 View 框架之前变得透明,以给人一种它向上和向下淡出的错觉。

关于如何实现这一目标有什么建议吗?

最佳答案

天啊,我经常用这个。将其保存为片段:

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;

此解决方案要求将 TextView 嵌入到其自己的 View 中。这会对 TextView 的顶部和底部应用 3% 的淡入淡出。根据您的需要修改 gradient.locations

关于ios - 在图像上淡出滚动 UITextView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59947050/

相关文章:

iphone - renderInContext 创建未及时释放的内存

iphone - iOS 灰度/黑白

ios - 无法将渐变添加到 UIViewController 中的 UIImageView

iOS 在 doBecomeActive 后取消暂停游戏

ios - 应用未运行时如何处理远程通知

ios - 分配给只读属性错误 iOS 6

ios - 关闭然后打开 iOS 应用程序到特定的 ViewController

ios - 如何将柔和形状的阴影应用于其中具有透明区域的图形?

ios - CAGradientLayer 无法正常工作

iOS 以编程方式渐变