2016-11-17 3 views
2

J'apprécierais vraiment, si quelqu'un pourrait me dire pourquoi le code ci-dessous ne me donne pas l'ombre intérieure, ou me donner une solution qui donnera une ombre intérieure.Swift Créer une ombre intérieure sur un UIView rond

J'ai besoin de créer une ombre intérieure sur un UIView arrondi. J'ai été à travers de nombreuses réponses et j'ai trouvé des façons d'obtenir ceci sur un UIViews carré normal, mais j'ai trouvé aucune solution qui fonctionne sur une vue arrondie. Au lieu de cela, je trouve des solutions comme celle ci-dessous qui me semblent correctes, mais qui ne créent pas l'ombre interne requise lorsque je les implémente. Voici mon écran, il est le point de vue blanc entre les vues jaune bleu et intérieure extérieure que je veux ajouter l'ombre à:

outer

Je sous-classé la vue, voici mon code tirage rect:

let innerShadow = CALayer() 
    // Shadow path (1pt ring around bounds) 
    let path = UIBezierPath(rect: innerShadow.bounds.insetBy(dx: -1, dy: -1)) 
    let cutout = UIBezierPath(rect: innerShadow.bounds).reversing() 
    path.append(cutout) 
    innerShadow.shadowPath = path.cgPath 
    innerShadow.masksToBounds = true 
    // Shadow properties 
    innerShadow.shadowColor = UIColor.darkGray.cgColor 
    innerShadow.shadowOffset = CGSize(width: 0.0, height: 7.0) 
    innerShadow.shadowOpacity = 1 
    innerShadow.shadowRadius = 5 
    // Add 
    self.layer.addSublayer(innerShadow) 

    // Make view round 
    self.layer.cornerRadius = self.frame.size.width/2 
    self.layer.masksToBounds = true 

Un grand merci pour toute aide à ce sujet. S'il vous plaît faites le moi savoir si vous avez des questions.

Répondre

0

Je viens de trouver ce hier

Masque un cercle du centre de la vue bleu

let maskLayer = CAShapeLayer() 

    // based on the image the ring is 1/6 its diameter 
    let radius = self.bounds.width * 1.0/6.0 
    let path = UIBezierPath(rect: self.bounds) 
    let holeCenter = CGPoint(x: center.x - (radius * 2), y: center.y - (radius * 2)) 

    path.addArc(withCenter: holeCenter, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true) 

    maskLayer.path = path.cgPath 
    maskLayer.fillRule = kCAFillRuleEvenOdd 

    blueView.layer.mask = maskLayer 

ci-dessus vous donnera un anneau bleu.

Ensuite créer un blackView qui agira comme notre ombre

var blackView = UIView() 

Définir son cadre pour être le même que le point de vue bleu.

blackView.frame = blueView.frame 
blackView.clipToBounds = true 

Découpez un trou similaire de la blackView

let maskLayer = CAShapeLayer() 

    // This is the most important part, the mask shadow allows some of the black view to bleed from under the blue view and give a shadow 
    maskLayer.shadowOffset = CGSize(width: shadowX, height: shadowY) 
    maskLayer.shadowRadius = shadowRadius 

    let radius = self.bounds.width * 2.0/6.0 
    let path = UIBezierPath(rect: self.bounds) 
    let holeCenter = CGPoint(x: center.x - (radius * 2), y: center.y - (radius * 2)) 
    path.addArc(withCenter: holeCenter, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true) 

    maskLayer.path = path.cgPath 
    maskLayer.fillRule = kCAFillRuleEvenOdd 

    blackView.layer.mask = maskLayer