代码之家  ›  专栏  ›  技术社区  ›  swiftPunk Alexander

如何在SwiftUI中创建移动/活动虚线?

  •  0
  • swiftPunk Alexander  · 技术社区  · 4 年前

    struct ContentView: View {
        
        var body: some View {
            
            Rectangle()
                .strokeBorder(style: StrokeStyle(lineWidth: 5, dash: [10]))
                .frame(width: 200, height: 200)
                .foregroundColor(Color.blue)
            
        }
        
    }
    

    enter image description here

    1 回复  |  直到 4 年前
        1
  •  2
  •   rob mayoff    4 年前

    动画化 dashPhase StrokeStyle

    struct ContentView: View {
        @State var phase: CGFloat = 0
        
        var body: some View {
            Rectangle()
                .strokeBorder(
                    style: StrokeStyle(
                        lineWidth: 5,
                        dash: [10],
                        dashPhase: phase))
                .animation(
                    Animation.linear(duration: 1)
                        .repeatForever(autoreverses: false),
                    value: phase)
                .frame(width: 200, height: 200)
                .foregroundColor(Color.blue)
                .onAppear {
                    phase = 20
                }
        }
    }