J'ai créé un UIView personnalisé dans mon projet iOS qui a une ombre portée. Mon objectif est d'appliquer le même dégradé à l'ombre que sur l'arrière-plan de la vue.UIView Shadow Gradient
Voici un exemple de l'aspect de mes ombres de couleur unie.
Cela se fait par une sous-classe de UIView avec le code ci-dessous:
override func layoutSubviews() {
let gradientLayer = layer as! CAGradientLayer
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
layer.cornerRadius = cornerRadius
layer.shadowColor = shadowColor.cgColor
layer.shadowOffset = CGSize(width: shadowX, height: shadowY)
layer.shadowRadius = shadowBlur
layer.shadowOpacity = 1
let inset: CGFloat = bounds.width * 0.05
layer.shadowPath = UIBezierPath(roundedRect: bounds.insetBy(dx: inset, dy: 0.0), cornerRadius: cornerRadius).cgPath
}
Je joue autour de la création d'une deuxième couche de gradient et de masquage à l'ombre, mais ont pas eu de chance. S'il te plait, oriente moi dans la bonne direction!