Created
November 4, 2023 19:17
-
-
Save keetsta/dabe67c706af7f30c2ab20e709228feb to your computer and use it in GitHub Desktop.
SwiftUI multicolor animated stroke
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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