Swift 在2个CG点之间绘制线,而不使用多个addLine

Swift 在2个CG点之间绘制线,而不使用多个addLine,swift,path,swiftui,shapes,Swift,Path,Swiftui,Shapes,我正在SwiftUI中尝试在CGPointx:0,y:0,CGPointx:100,y:100之间画一条线 有没有什么方法可以代替最简单的方法来指定4 CGPoint,我可以从第1点到第2点这样说?谢谢。你好像忘了画线。填充直线将填充直线所包围的区域,这就是为什么始终需要4个点的原因 你只需要2分: struct Line : Shape { func path(in rect: CGRect) -> Path { var path = Path()

我正在SwiftUI中尝试在CGPointx:0,y:0,CGPointx:100,y:100之间画一条线


有没有什么方法可以代替最简单的方法来指定4 CGPoint,我可以从第1点到第2点这样说?谢谢。

你好像忘了画线。填充直线将填充直线所包围的区域,这就是为什么始终需要4个点的原因

你只需要2分:

struct Line : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 100, y: 100))
        return path
    }
}
用法这是您笔划的地方:

使用笔划和填充属性

struct Line : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 100, y: 100))
        return path
    }
}
struct GpsView: View {
    var body: some View {
        Line()
            .stroke(lineWidth: 5.0)
            .fill(Color.red)
            .frame(width: 300, height: 300)
    }
}

我不明白。为什么需要指定4个点,而不是您想要的2个点?我也尝试过只指定2个点,path.moveto:CGPointx:0,y:0和path.addLineto:CGPointx:0,y:100,但我在屏幕上看不到任何东西。。我忘记了一些修饰语…我找不到原因。。。
struct Line : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 100, y: 100))
        return path
    }
}
Line()
    .stroke(Color.red, lineWidth: 5)
    .frame(width: 300, height: 300)
struct Line : Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: 100, y: 100))
        return path
    }
}
struct GpsView: View {
    var body: some View {
        Line()
            .stroke(lineWidth: 5.0)
            .fill(Color.red)
            .frame(width: 300, height: 300)
    }
}