0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】Pathで任意の図形を描画する

Posted at

概要

  • Pathを用いることで、任意の図形を描画することができる。

コード

struct Triangle: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()

     //今回は3角形。呼び出し元の矩形の頂点情報をもとに座標を指定する
        let p1 = CGPoint(x: rect.midX, y: rect.minY)
        let p2 = CGPoint(x: rect.maxX, y: rect.maxY)
        let p3 = CGPoint(x: rect.minX, y: rect.maxY)

        path.move(to: p1) 
        path.addLine(to: p2) 
        path.addLine(to: p3) 
        path.closeSubpath()

        return path
    }
}
// 呼び出し元
Triangle() 
    .frame(width: 200, height: 200)

解説

  • Pathを用いて(座標で描画できるものであれば)任意の形を描画できる。
  • move(to: )で始点を指定し、その後のaddLine()で指定した座標を2点結ぶ直線を描画していく。
  • 最後にclostSubPath()を使ってパスを閉じる
  • 呼び出し元のframeでサイズを指定することができる。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?