问题描述
考虑到iOS上的任意UIView,是否有一种方法可以使用Core Graphics(CAGradientLayer)来应用前景透明渐变?
Given an arbitrary UIView on iOS, is there a way using Core Graphics (CAGradientLayer comes to mind) to apply a "foreground-transparent" gradient to it?
我不能使用标准的CAGradientLayer,因为背景比UIColor更复杂。我也无法覆盖PNG,因为背景会随着我的子视图沿其父垂直滚动视图滚动而变化(见图片)。
I can't use a standard CAGradientLayer because the background is more complex than a UIColor. I also can't overlay a PNG because the background will change as my subview is scrolled along its parent vertical scrollview (see image).
我有一个非优雅的后备:让我的uiview剪辑其子视图,并在滚动父滚动视图时移动背景的预渲染渐变png。
I have a non-elegant fallback: have my uiview clip its subviews and move a pre-rendered gradient png of the background as the parent scrollview is scrolled.
推荐答案
这是一个令人尴尬的简单修复:应用CAGradientLayer作为我的子视图的掩码。
This was an embarrassingly easy fix: apply a CAGradientLayer as my subview's mask.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = _fileTypeScrollView.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
gradientLayer.startPoint = CGPointMake(0.8f, 1.0f);
gradientLayer.endPoint = CGPointMake(1.0f, 1.0f);
_fileTypeScrollView.layer.mask = gradientLayer;
感谢让我指向正确的方向!
Thanks to Cocoanetics for pointing me in the right direction!
这篇关于UIView透明渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!