SwiftUI combine rectangle and triangle in shape

Making custom shape is not hard in SwiftUI by implementing func path(in rect: CGRect) -> Path. You just have to draw points in your desired location in 2D coordinate space by path.move() and path.addLine() func, a line will be drawn automatically.

struct MomentumArrow: Shape {
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        let squareHeight = rect.height * 0.65
        
        // Rect
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 0, y: squareHeight))
        path.addLine(to: CGPoint(x: rect.width, y: squareHeight))
        path.addLine(to: CGPoint(x: rect.width, y: 0))
        path.addLine(to: CGPoint(x: 0, y: 0))
        
        // Triangle
        path.move(to: CGPoint(x: 0, y: squareHeight))
        path.addLine(to: CGPoint(x: rect.midX, y: rect.height))
        path.addLine(to: CGPoint(x: rect.width, y: squareHeight))
    
        return path
    }
    
}
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a comment