2016-09-20 13 views
2

に裏打ちされたのUIViewを持つ三角形:http://swiftiostutorials.com/tutorial-draw-nice-triangle-view-border-cashapelayer/は、私のような三角形を作成するために管理:形状は、チュートリアルから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 
    } 
} 

しかし、私は何を達成しようとしているが、のような三角形であります:

enter image description here

1がこれをどのように行うのですか?代わりに

答えて

3

使用このパスを:

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

完全クラス:

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

ああ、余分な 'CGPathAddLineToPoint'に気付きませんでした。ありがとう ! – Orange

関連する問題