2016-09-20 1 views
2

aus dem Tutorial: http://swiftiostutorials.com/tutorial-draw-nice-triangle-view-border-cashapelayer/ schaffte ich es wie ein Dreieck zu erstellen:Form ein Dreieck mit UIView unterstützt von CAShapeLayer

enter image description here

class Triangle: UIView { 
    override func drawRect(rect: CGRect) { 
     let mask = CAShapeLayer() 
     mask.frame = self.layer.bounds 

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

     let path = CGPathCreateMutable() 

     CGPathMoveToPoint(path, nil, 0, 0) 
     CGPathAddLineToPoint(path, nil, width, 0) 
     CGPathAddLineToPoint(path, nil, width, height) 
     CGPathAddLineToPoint(path, nil, width/2, height) 
     CGPathAddLineToPoint(path, nil, width, height) 


     mask.path = path 
     self.layer.mask = mask 
    } 
} 

Aber was ich versuche zu erreichen, ist ein Dreieck wie :

enter image description here

Wie man das macht?

Antwort

3

Verwenden Sie diesen Pfad statt:

CGPathMoveToPoint(path, nil, 0, 0) 
CGPathAddLineToPoint(path, nil, width, 0) 
CGPathAddLineToPoint(path, nil, width/2, height) 
CGPathAddLineToPoint(path, nil, 0, 0) 

volle Klasse:

class Triangle: UIView { 
    override func drawRect(rect: CGRect) { 
     let mask = CAShapeLayer() 
     mask.frame = self.layer.bounds 

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

     let path = CGPathCreateMutable() 

     CGPathMoveToPoint(path, nil, 0, 0) 
     CGPathAddLineToPoint(path, nil, width, 0) 
     CGPathAddLineToPoint(path, nil, width/2, height) 
     CGPathAddLineToPoint(path, nil, 0, 0) 

     mask.path = path 
     self.layer.mask = mask 
    } 
} 
+0

Ahh, nicht den zusätzlichen 'CGPathAddLineToPoint' bemerkt hat. Danke, Kumpel! – Orange

Verwandte Themen