我的设计是这样的:
我有背景图片。在背景图片的顶部,我有UIViews
,它的渐变颜色是透明的,可以显示背景图片。
如何实现?
我做了以下事情:
UIView
以显示渐变。我尝试了以下代码:
extension UIView {
func applyGradientWithTransparancy(colours: [UIColor], withTranparancy: CGFloat) -> Void {
self.applyGradient(colours: colours, locations: nil, withTranparancy: withTranparancy )
}
func applyGradient(colours: [UIColor], locations: [NSNumber]?, withTranparancy: CGFloat) -> Void {
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.withAlphaComponent(withTranparancy).cgColor }
gradient.locations = locations
gradient.startPoint = CGPoint(x: 0.0, y: 0.5);
gradient.endPoint = CGPoint(x: 1.0, y: 0.5);
// self.layer.addSublayer(gradient)
self.layer.masksToBounds = true
self.layer.insertSublayer(gradient, at: 0)
}
}
func setGradients(){
let startColor = UIColor(hex: "#0c8674")
let endColor = UIColor(hex: "#0d7982")
let startColor1 = UIColor(hex: "#90543c")
let endColor1 = UIColor(hex: "#88354b")
let startColor2 = UIColor(hex: "#ffad01")
let endColor2 = UIColor(hex: "#fd8247")
secondView.applyGradientWithTransparancy(colours: [startColor!,endColor!], withTranparancy: 0.25)
thirdView.applyGradientWithTransparancy(colours: [startColor1!,endColor1!], withTranparancy: 0.4)
fourthView.applyGradientWithTransparancy(colours: [startColor2!,endColor2!], withTranparancy: 1.0)
}
我看不到渐变。
我会得到什么
我期望