2016-06-01 15 views
6

Sto creando una vista personalizzata che voglio mascherare e aggiungere un'ombra ad esso.Come mascherare e aggiungere ombra ad un UIView

il mascheramento:

let p = UIBezierPath() 
    p.moveToPoint(CGPointMake(20, 20)) 
    p.addLineToPoint(CGPointMake(100, 20)) 
    p.addLineToPoint(CGPointMake(100, 50)) 
    p.addLineToPoint(CGPointMake(110, 55)) 
    p.addLineToPoint(CGPointMake(100, 60)) 
    p.addLineToPoint(CGPointMake(100, 100)) 
    p.addLineToPoint(CGPointMake(20, 100)) 
    p.closePath() 

    let s = CAShapeLayer() 
    s.frame = layer.bounds 
    s.path = p.CGPath 
    s.fillColor = UIColor.greenColor().CGColor 
    layer.mask = s 

il mascheramento funziona, ora voglio aggiungere un'ombra. ma non funziona.

ho provato ad aggiungere ombra al livello principale e non succede nulla.

layer.shadowColor = UIColor.yellowColor().CGColor 
    layer.shadowRadius = 10 
    layer.shadowOpacity = 0.9 
    layer.shadowOffset = CGSizeZero 

ho provato ad aggiungerlo al livello maschera e ho ottenuto la vista principale mascherata con un'ombra.

s.shadowColor = UIColor.yellowColor().CGColor 
    s.shadowRadius = 10 
    s.shadowOpacity = 0.9 
    s.shadowOffset = CGSizeZero 

Qualche suggerimento su come aggiungere questa ombra gialla alla vista mascherata?

Grazie

+0

Assicurarsi che il vista does't avere un quadro – iSashok

+0

a zero Esso non posso vedere la vista, ma non c'è ombra – ilan

+1

hai fatto vuoi davvero una maschera non un sottolivello? –

risposta

4

Grazie @WilsonXJ ho cambiato la maschera al addSubLayer.

Questa è la risposta che ha funzionato per me:

let p = UIBezierPath() 
    p.moveToPoint(CGPointMake(20, 20)) 
    p.addLineToPoint(CGPointMake(100, 20)) 
    p.addLineToPoint(CGPointMake(100, 50)) 
    p.addLineToPoint(CGPointMake(110, 55)) 
    p.addLineToPoint(CGPointMake(100, 60)) 
    p.addLineToPoint(CGPointMake(100, 100)) 
    p.addLineToPoint(CGPointMake(20, 100)) 
    p.closePath() 

    let s = CAShapeLayer() 
    s.fillColor = UIColor.whiteColor().CGColor 
    s.frame = layer.bounds 
    s.path = p.CGPath 

    layer.backgroundColor = UIColor.clearColor().CGColor 
    layer.addSublayer(s) 

    layer.masksToBounds = true 
    layer.shadowColor = UIColor.yellowColor().CGColor 
    layer.shadowOffset = CGSizeZero 
    layer.shadowOpacity = 0.9 
    layer.shadowPath = p.CGPath 
    layer.shadowRadius = 10 
+0

Le sottoview si nascondono ,,,, – Dhiru

+0

Per Swift4: https://stackoverflow.com/a/46512426/2033195 –

Problemi correlati