iphone - UIView透明渐变

标签 iphone ios core-graphics cagradientlayer

给定一个 iOS 上的任意 UIView,有没有办法使用 Core Graphics(想到 CAGradientLayer)对其应用“前景透明”渐变?

我不能使用标准的 CAGradientLayer,因为背景比 UIColor 更复杂。我也无法覆盖 PNG,因为背景会随着我的 subview 沿其父垂直 ScrollView 滚动而改变(见图)。

我有一个不优雅的回退:让我的 uiview 剪辑它的 subview ,并在滚动父 ScrollView 时移动背景的预渲染渐变 png。

transparent uiview gradient problem

最佳答案

这是一个令人尴尬的简单修复:应用 CAGradientLayer 作为我的 subview 的 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;

感谢Cocoanetics为我指明了正确的方向!

关于iphone - UIView透明渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38268327/

相关文章:

iphone - 在 iPhone 上运行时内存泄漏

javascript - Phonegap 获取 App 版本返回 null

ios - "System unavailable please try later "- iPhone 中的 PAYPAL MPL SDK 错误

html - -webkit-溢出-滚动 : touch kills overflow: hidden

iOS:自动布局中的多行 UILabel

iphone - -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization] 错误

ios - 将 mask 应用于 UIView 以模拟玻璃微光

iphone - 如何将 'Flatten' 多个 UIImageView 合并为一个?

iphone - UITableView 滚动是否以一定的速度平滑?

ios - 如何组合过滤器