salut j'aime ajouter une ombre de Calayer à une vue où l'ombre était laissée et à droite de la vue le moyen le plus simple était la suivante: mais je vais ajouter une plus grande ombre comme un Shadow quand j'augmente le Shadowradius, il n'est pas beau. Comment puis-je faire une ombre qui a l'air bien à gauche et à droite. P> p>
4 Réponses :
Je pense que 10 est un assez gros rayon d'ombre, essayez plutôt 3 ou 4, aussi opacité que j'utilise habituellement 0,7: si vous voulez l'ombre seulement à gauche et à droite, puis inset. Le rectangle sur le dessus et le bas de sorte que l'ombre supérieure et inférieure est cachée derrière votre vue: p> Je ne suis pas vraiment sûr que c'est ce que tu voulais. P> p>
J'ai parlé avec mon designer Cette ombre est trop petite. J'ai besoin de différents compensations x à gauche et à droite.
Réponse incroyable, mais les gars ont quelqu'un qui a mis en œuvre cela avec des coins arrondis (plus lisses), puisque cette ombre ressemblant à un rectocole est mauvaise. Des idées? Merci d'avance ! :)
Utilisez BezierPathwithRoundeRect à la place
Je ne sais pas pourquoi cela a tant de votes. Dans ma mise en œuvre, comme prévu, il empêche les ombres latérales d'aller jusqu'au sommet et au bas des côtés.
@ProgmR Je veux une ombre intérieure avec un côté spécifique, comme le haut ou le haut / le haut / droite. Aucune suggestion ?
La réponse de ProgMr était très utile, acclamations!
J'ai fait un menu de glissière et j'ai eu un problème avec l'ombre entourant mon VC et perturber la barre de navigation. Il s'est avéré que je devais insérer la couche d'ombre. P>
Voici ma solution à l'aide de SWIFT: strong> p> rightViewController!.view.layer.shadowOpacity = 0.8
rightViewController!.view.layer.shadowOffset = CGSizeMake(0, 3)
rightViewController!.view.layer.shadowRadius = 4.0
let shadowRect: CGRect = CGRectInset(rightViewController!.view.bounds, 0, 4); // inset top/bottom
rightViewController!.view.layer.shadowPath = UIBezierPath(rect: shadowRect).CGPath
imageView.layer.shadowOpacity = 0.8
imageView.layer.shadowOffset = CGSize(width: 0, height: 3)
imageView.layer.shadowRadius = 4.0
let shadowRect: CGRect = imageView.bounds.insetBy(dx: 0, dy: 4)
imageView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
J'ai créé une ombre pouvant être utilisée en haut / bas ou à gauche / à droite, sans préjudice du tout. Aucune ombre n'apparaîtra dans la gauche / droite si vous choisissez le haut / le bas, etc. J'espère que j'ai aidé. Voici le code:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let imgView = UIImageView(frame: CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY - 150, width: 300, height: 300)) imgView.contentMode = .scaleToFill imgView.image = UIImage(named: "name image") self.view.addSubview(imgView) //You can put left/right or top/bottom imgView.addShadow(sides: .leftRight, constant: 10, alpha: 5) } } extension UIView{ func addShadow(sides: Sides, constant: Int, alpha: Int){ if(constant > 0 && alpha > 0){ switch sides { case .leftRight: let view = UIView(frame: CGRect(x: -CGFloat(constant), y: 0, width: self.bounds.width + CGFloat((constant * 2)), height: self.bounds.height)) self.addSubview(view) addMask(sides: .leftRight, view: view, constant: constant, shadowRadius: alpha) case .topBottom: let view = UIView(frame: CGRect(x: 0, y: -CGFloat(constant), width: self.bounds.width , height: self.bounds.height + CGFloat(constant * 2))) self.addSubview(view) addMask(sides: .topBottom, view: view, constant: constant, shadowRadius: alpha) } } } private func addMask(sides:Sides, view: UIView, constant: Int, shadowRadius:Int){ let mutablePath = CGMutablePath() let mask = CAShapeLayer() switch sides { case .leftRight: mutablePath.addRect(CGRect(x: -CGFloat(constant), y: 0, width: view.bounds.width, height: view.bounds.height)) mutablePath.addRect(CGRect(x: CGFloat(constant) , y: 0, width: view.bounds.width , height: view.bounds.height)) case .topBottom: mutablePath.addRect(CGRect(x: 0, y: -CGFloat(constant), width: view.bounds.width, height: view.bounds.height)) mutablePath.addRect(CGRect(x: 0, y: CGFloat(constant) , width: view.bounds.width , height: view.bounds.height)) } mask.path = mutablePath mask.fillRule = CAShapeLayerFillRule.evenOdd mask.fillColor = UIColor(white:1.0, alpha: 0.2).cgColor view.layer.mask = mask view.layer.addSublayer(mask) mask.shadowColor = UIColor.black.cgColor mask.shadowRadius = CGFloat(shadowRadius) mask.shadowOpacity = 1.0 mask.shadowOffset = CGSize(width: 0, height: 0) } } enum Sides{ case leftRight case topBottom }