2
3

More than 1 year has passed since last update.

【SwiftUI】よくある感じのくるくるインジケーター

Last updated at Posted at 2023-03-02

Simulator-Screen-Recording-iPhone-14-Pro-2023-03-02-at-19.29.13.gif

struct SpinnerView: View {
    @State private var showSpinner = false
    @State private var degree: Int = 270
    @State private var spinnerLength = 0.6
    
    var body: some View {
        ZStack {
            VStack {
                Button {
                    showSpinner.toggle()
                } label: {
                    Text("show spinner")
                }
            }
            if showSpinner {
                Circle()
                    .trim(from: 0.0, to: spinnerLength)
                    .stroke(.green, style: StrokeStyle(lineWidth: 8.0, lineCap: .square, lineJoin: .round))
                    .animation(Animation.easeIn(duration: 1.5).repeatForever(autoreverses: true), value: spinnerLength)
                    .frame(width: 60, height: 60)
                    .rotationEffect(Angle(degrees: Double(degree)))
                    .animation(Animation.linear(duration: 1).repeatForever(autoreverses: false), value: degree)
                    .onAppear {
                        degree = 270 + 360
                        spinnerLength = 0
                    }
            }
        }
    }
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3