如何在SwiftUI中做出合适的曲线?

问题描述 投票:0回答:1

我是新来以编程方式绘制paths的,我的项目要求如下。 (忘记红色矩形,它仅用于审查目的)。

我当前的代码就像

import SwiftUI

struct CurveShape: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: .zero)
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
        path.addCurve(to: CGPoint(x: rect.minX, y: rect.maxY),
                      control1: CGPoint(x: rect.maxX * 0.70, y: rect.midY),
                      control2: CGPoint(x: rect.maxX * 0.25, y: rect.maxY * 1.25))
        path.closeSubpath()
        return path
    }
}

struct CurveShape_Previews: PreviewProvider {
    static var previews: some View {
        CurveShape()
            .stroke(Color.red, lineWidth: 5)
            .frame(height: 200)
            .padding()
    }
}

当前结果是

无论如何要正确地做?

swift swiftui shapes
1个回答
0
投票

这是一种方法:

var body: some View {
    VStack(spacing: 0) {
        Color.blue
            .cornerRadius(50)
            .padding(.trailing, 20)
            .padding(.leading, -50)
            .padding(.top, -50)
            .background(.yellow)
        Color.yellow
            .cornerRadius(50)
            .padding(.leading, 20)
            .padding(.trailing, -50)
            .padding(.bottom, -50)
            .background(.blue)
    }
    .clipped()
    .ignoresSafeArea()
}

Screenshot

© www.soinside.com 2019 - 2024. All rights reserved.