Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI-使用SwiftUI创建一条虚线_Ios_Swift_Xcode_Swiftui - Fatal编程技术网

Ios SwiftUI-使用SwiftUI创建一条虚线

Ios SwiftUI-使用SwiftUI创建一条虚线,ios,swift,xcode,swiftui,Ios,Swift,Xcode,Swiftui,我需要创建一条虚线。我试着创建一个带有虚线的矩形视图。但是,当将矩形的高度设置为1时,将产生一条双线作为其显示视图的上边框和下边框 代码如下: Rectangle() .fill(Color.clear) .frame(height: 1, alignment: .bottom) .overlay( RoundedRectangle(cornerRadius: 0) .stroke(style: StrokeStyle(lineWi

我需要创建一条虚线。我试着创建一个带有虚线的矩形视图。但是,当将矩形的高度设置为1时,将产生一条双线作为其显示视图的上边框和下边框

代码如下:

Rectangle()
    .fill(Color.clear)
    .frame(height: 1, alignment: .bottom)
    .overlay(
        RoundedRectangle(cornerRadius: 0)
            .stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))
            .foregroundColor(Color(UIColor.blue))
    )

根据您想做的事情,您可以这样做:

VStack {
    Path{ path in
        path.move(to: CGPoint(x: 20, y: 300))
        path.addLine(to: CGPoint(x: 200, y: 300))
    }
    .stroke(style: StrokeStyle( lineWidth: 10, dash: [5]))
    .foregroundColor(Color(UIColor.blue))
}
你会得到这样的结果:

更改破折号值以增加或减少行中的破折号

struct ContentView: View {
     var body: some View {
         Line()
           .stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))
           .frame(height: 1)
    }
}

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: rect.width, y: 0))
        return path
    }
}
结果:


改进的@kazi.munshimun解决方案。垂直线和水平线:

struct VLine: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: rect.midX, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
        }
    }
}

struct HLine: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: rect.minX, y: rect.midY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
        }
    }
}
用法:

VLine().stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))
HLine().stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))

使用此解决方案,线条的坐标是固定的,因此当线条实际放置在应用程序UI中时,根据屏幕大小,线条位于不同的位置。没有硬编码坐标,我们怎么能做到这一点?我需要将其放置在两个其他视图(在HStack中)之间并居中。@Tdg10e它们对于给定视图是固定的。您可以使用该代码并在该行中移动,就像使用填充、偏移、间隔等移动任何其他内容(Text()、Image())一样。是否可以倾斜虚线?是的,更干净的解决方案,就像我们使用
rect
动态大小一样。然而,只有一件小事。。。与其称为
虚线
,不如称为
虚线
:p感谢您的解决方案:)如何使其垂直?@staticVoidMan应称为
HLine
:P@Nikaaner:是的<代码>HLine