Skip to content

Instantly share code, notes, and snippets.

@keetsta
Created November 4, 2023 19:17
Show Gist options
  • Save keetsta/dabe67c706af7f30c2ab20e709228feb to your computer and use it in GitHub Desktop.
Save keetsta/dabe67c706af7f30c2ab20e709228feb to your computer and use it in GitHub Desktop.
SwiftUI multicolor animated stroke
struct DashedRect: View {
@State private var dashAngle: CGFloat = 0
@State private var dashAngle2: CGFloat = 10
var body: some View {
VStack {
HStack {
}
}
.frame(maxWidth: .infinity, alignment: .center)
.background(.gray)
.cornerRadius(20)
.padding(.all, 25)
.background {
RoundedRectangle(cornerRadius: 20)
.strokeBorder(
style: StrokeStyle(
lineWidth: 3,
dash: [10],
dashPhase: dashAngle))
.animation(.linear(duration: 1).repeatForever(autoreverses: false), value: dashAngle)
.foregroundColor(.purple.opacity(0.8))
.onAppear {
dashAngle = 20
}
RoundedRectangle(cornerRadius: 20)
.strokeBorder(
style: StrokeStyle(
lineWidth: 3,
dash: [10],
dashPhase: dashAngle2))
.animation(
.linear(duration: 1).repeatForever(autoreverses: false), value: dashAngle2)
.foregroundColor(.pink.opacity(0.5))
.onAppear {
dashAngle2 = 30
}
}
}
}
#Preview {
DashedRect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment