ios - SwiftUI - 如何使用 Shape 制作垂直虚线?

标签 ios swiftui shapes ios14

struct DottedLine: 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
    }
}

DottedLine()
                .stroke(style: StrokeStyle(lineWidth: 1, dash: [2]))
                .frame(height: 1)
                .foregroundColor(Color.red)

这将创建一条水平虚线。但是如何让它垂直呢?如果我将 Divider() 放在 HStack 中,那么它会变成垂直的,但是如何使用虚线实现相同的效果?

最佳答案

使 DottedLine 对宽度和高度使用react,并根据需要进行配置以代替使用:

demo

struct DottedLine: 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: rect.height))
        return path
    }
}

struct TestDottedLineView: View {
    var body: some View {
        DottedLine()
            .stroke(style: StrokeStyle(lineWidth: 1, dash: [2]))
            .frame(width: 1, height: 100)
            .foregroundColor(Color.red)
    }
}

关于ios - SwiftUI - 如何使用 Shape 制作垂直虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65248710/

相关文章:

ios - 如何在 segue 中传递对象而不是对象的细节?

ios - 如何将视频 URL 数组保存到用户默认值?

android - 在 list 中定义 sdk-version 后色带解决方案被杀死

python - 形状分析以区分矩形与其他形状

ios - UITableViewCell 仅在滚动时加载内容

ios - 在 iOS 中跨应用程序(后台/前台或分屏多任务处理)共享 UDP 数据

ios - 使用 URLSession 为 SwiftUI View 加载 JSON 数据

ios - UIKit 中的可扩展 SwiftUI View 垂直居中

swift - 如何为 Widget 配置动态值设置 TableView 样式?

java - 使用计时器和开始按钮移动形状对象数组 - Java