Last active
July 26, 2025 18:59
-
-
Save jsmmth/b78a5586a2b88b86ce45bacbc950a114 to your computer and use it in GitHub Desktop.
SwiftUI Animated Dashed Border
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
import SwiftUI | |
struct AnimatedBorder: View { | |
@State private var phase: CGFloat = 0 | |
let cornerRadius: CGFloat = 25 | |
let dashPattern: [CGFloat] = [15, 15] | |
let lineWidth: CGFloat = 4 | |
var body: some View { | |
ZStack { | |
RoundedRectangle(cornerRadius: cornerRadius) | |
.fill(Color(uiColor: UIColor(displayP3Red: 0.96, green: 1, blue: 0.97, alpha: 1))) | |
.frame(width: 280, height: 90) | |
RoundedRectangle(cornerRadius: cornerRadius) | |
.stroke( | |
Color(red: 0.82, green: 0.9, blue: 0.83), | |
style: StrokeStyle( | |
lineWidth: lineWidth, | |
lineCap: .round, | |
lineJoin: .round, | |
dash: dashPattern, | |
dashPhase: phase | |
) | |
) | |
.frame(width: 276, height: 86) | |
.animation( | |
Animation.linear(duration: 1) | |
.repeatForever(autoreverses: false), | |
value: phase | |
) | |
.onAppear { | |
phase = dashPattern.reduce(0, -) | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
VStack() { | |
Spacer() | |
AnimatedBorder() | |
Spacer() | |
} | |
.padding() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment