2017-08-02 7 views
0

Ich habe ein Maskenlayout für meine Ansicht zu erstellen, aber ich kann nicht das Ergebnis, das ich bekommenfür UIView mit CAShapeLayer()

@IBOutlet weak var viewToCrop: UIView! 
let angle = CGFloat(2*M_PI/3) 

func createMask() { 
    let mask = CAShapeLayer() 
    mask.frame = viewToCrop.layer.bounds 
    let heightButton = viewToCrop.frame.height 
    let lineWidth = -heightButton/tan(angle) 

    let width = viewToCrop.layer.frame.size.width 
    let height = viewToCrop.layer.frame.size.height 

    let path = CGPathCreateMutable() 
    CGPathMoveToPoint(path, nil, 0, 0) 
    CGPathAddLineToPoint(path, nil, width, 0) 
    CGPathMoveToPoint(path, nil, width - lineWidth, height) 
    CGPathAddLineToPoint(path, nil, lineWidth, height) 
    CGPathAddLineToPoint(path, nil, 0, 0) 

    mask.path = path 
    viewToCrop.layer.mask = mask 
} 

Ich muss erhalten solche Figur erstellen:

enter image description here

jedoch nach mir Code ausgeführt erhalte ich die rechteckig. Was mache ich falsch?

P.S. Ich arbeite an einem älteren Projekt auf Swift 2

+0

Welchen Wert Winkel hier ist? – agibson007

+0

60 Grad @ agibson007 –

+0

auch warum Swift 2? – agibson007

Antwort

1

Ich habe keine Swift 2 Compiler-Option, aber ich werde einen Stich zu nehmen und hoffentlich dir nahe kommen. Das Größte, was ich denke, ist, dass der Pfad lineWidth für die Koordinaten durch 2 geteilt werden muss. Eine andere Sache zu beachten ist, dass, wenn die Höhe der Ansicht zu groß ist, 60 zu steil ist. Ich würde vorschlagen, es basierend auf einem Prozentsatz zu zeichnen, es sei denn, Sie brauchen den genauen Winkel. Habe mein Bestes gegeben ohne einen Swift 2 Compiler, lass es mich wissen.

@IBOutlet weak var viewToCrop: UIView! 
let angle = CGFloat(2*M_PI/3) 

func createMask() { 
    let mask = CAShapeLayer() 
    mask.frame = viewToCrop.layer.bounds 
    let heightButton = viewToCrop.frame.width 
    let lineWidth = -heightButton/tan(angle) 

    let width = viewToCrop.layer.frame.size.width 
    let height = viewToCrop.layer.frame.size.height 

    let path = CGPathCreateMutable() 
    CGPathMoveToPoint(path, nil, 0, 0) 
    CGPathAddLineToPoint(path, nil, width, 0) 
    CGPathMoveToPoint(path, nil, width - lineWidth/2, height) 
    CGPathAddLineToPoint(path, nil, lineWidth/2, height) 
    CGPathAddLineToPoint(path, nil, 0, 0) 

    mask.path = path 
    viewToCrop.layer.mask = mask 
    } 

Ergebnis Result

Verwandte Themen