【发布时间】:2017-05-24 10:51:40
【问题描述】:
我正在使用 UIView 扩展将渐变层应用到 UIView。我需要在滚动表视图时在运行时更改渐变颜色。我使用滚动视图的 contentoffset 值来更新渐变颜色。
我尝试了什么: 我试图从超级图层中删除图层并使用新颜色创建一个新的渐变图层。但应用程序出现内存问题,用户界面有时会冻结。
是否可以在运行时更新 CAGradientLayer 渐变颜色?
extension UIView {
func applyGradient(withColours colours: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
self.layer.insertSublayer(gradient, at: 0)
}
}
【问题讨论】:
-
将渐变层保留在一个属性中并更新其颜色属性。
-
是的。它似乎在工作@Alistra。谢谢
标签: ios swift animation uiview cagradientlayer