tsonobe1 / imonit

0 stars 0 forks source link

custom rounded rectangle path #25

Closed tsonobe1 closed 1 year ago

tsonobe1 commented 2 years ago
//
//  TaskBlockPath.swift
//  imonit
//
//  Created by 薗部拓人 on 2022/09/28.
//

import SwiftUI

struct CustomShape: Shape {
    let radius: CGFloat = 40

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

        let tl = CGPoint(x: rect.minX, y: rect.minY)
        let tr = CGPoint(x: rect.maxX, y: rect.minY)
        let brs = CGPoint(x: rect.maxX, y: rect.maxY - radius)
        let brc = CGPoint(x: rect.maxX - radius, y: rect.maxY - radius)
        let bls = CGPoint(x: rect.minX + radius, y: rect.maxY)
        let blc = CGPoint(x: rect.minX + radius, y: rect.maxY - radius)

        path.move(to: tl)
        path.addLine(to: tr)
        path.addLine(to: brs)
        path.addRelativeArc(center: brc, radius: radius,
          startAngle: Angle.degrees(0), delta: Angle.degrees(90))
        path.addLine(to: bls)
        path.addRelativeArc(center: blc, radius: radius,
          startAngle: Angle.degrees(90), delta: Angle.degrees(90))

        return path
    }
}

struct CustomShape2: Shape {
    let radius: CGFloat = 40

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

        let tl = CGPoint(x: rect.minX, y: rect.minY)
        let tls = CGPoint(x: rect.maxX - radius, y: rect.minY)
        let tlc = CGPoint(x: rect.maxX - radius, y: rect.minY + radius)
        let brs = CGPoint(x: rect.maxX, y: rect.maxY - radius)
        let brc = CGPoint(x: rect.maxX - radius, y: rect.maxY - radius)
        let als = CGPoint(x: rect.minX + radius, y: rect.maxY)
        let alc = CGPoint(x: rect.minX + radius, y: rect.maxY - radius)

        path.move(to: tl)
        path.addLine(to: tls)
        path.addRelativeArc(center: tlc, radius: radius,
          startAngle: Angle.degrees(180), delta: Angle.degrees(180))
        path.addLine(to: brs)
        path.addRelativeArc(center: brc, radius: radius,
          startAngle: Angle.degrees(270), delta: Angle.degrees(180))
        path.addLine(to: als)
        path.addRelativeArc(center: alc, radius: radius,
          startAngle: Angle.degrees(90), delta: Angle.degrees(90))

        return path
    }
}

struct CustomShape3: Shape {
    let radius: CGFloat = 10

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

        let tl = CGPoint(x: rect.minX + radius, y: rect.minY)
        let tls = CGPoint(x: rect.maxX - radius, y: rect.minY)
        let tlc = CGPoint(x: rect.maxX - radius, y: rect.minY + radius)
        let brs = CGPoint(x: rect.maxX, y: rect.maxY - radius)
        let brc = CGPoint(x: rect.maxX - radius, y: rect.maxY - radius)
        let als = CGPoint(x: rect.minX + radius, y: rect.maxY)
        let alc = CGPoint(x: rect.minX + radius, y: rect.maxY - radius)
        let mls = CGPoint(x: rect.minX, y: rect.minY + radius)
        let mlc = CGPoint(x: rect.minX + radius, y: rect.minY + radius)

        path.move(to: tl)
        path.addLine(to: tls)
        path.addRelativeArc(center: tlc, radius: radius,
          startAngle: Angle.degrees(180), delta: Angle.degrees(180))
        path.addLine(to: brs)
        path.addRelativeArc(center: brc, radius: radius,
          startAngle: Angle.degrees(270), delta: Angle.degrees(180))
        path.addLine(to: als)
        path.addRelativeArc(center: alc, radius: radius,
          startAngle: Angle.degrees(90), delta: Angle.degrees(90))
        path.addLine(to: mls)
        path.addRelativeArc(center: mlc, radius: radius,
          startAngle: Angle.degrees(180), delta: Angle.degrees(270))

        return path
    }
}
tsonobe1 commented 2 years ago

参考にしたref https://adrianhall.github.io/swift/2020/05/03/swiftui-masks/