Activity indicator in SwiftUI

后端 未结 9 2007
忘了有多久
忘了有多久 2020-11-29 17:36

Trying to add a full screen activity indicator in SwiftUI.

I can use .overlay(overlay: ) function in View Protocol.

With this, I c

相关标签:
9条回答
  • 2020-11-29 18:29

    Try this:

    import SwiftUI
    
    struct LoadingPlaceholder: View {
        var text = "Loading..."
        init(text:String ) {
            self.text = text
        }
        var body: some View {
            VStack(content: {
                ProgressView(self.text)
            })
        }
    }
    

    More information about at SwiftUI ProgressView

    0 讨论(0)
  • 2020-11-29 18:36

    Activity indicator in SwiftUI

    
    import SwiftUI
    
    struct Indicator: View {
    
        @State var animateTrimPath = false
        @State var rotaeInfinity = false
    
        var body: some View {
    
            ZStack {
                Color.black
                    .edgesIgnoringSafeArea(.all)
                ZStack {
                    Path { path in
                        path.addLines([
                            .init(x: 2, y: 1),
                            .init(x: 1, y: 0),
                            .init(x: 0, y: 1),
                            .init(x: 1, y: 2),
                            .init(x: 3, y: 0),
                            .init(x: 4, y: 1),
                            .init(x: 3, y: 2),
                            .init(x: 2, y: 1)
                        ])
                    }
                    .trim(from: animateTrimPath ? 1/0.99 : 0, to: animateTrimPath ? 1/0.99 : 1)
                    .scale(50, anchor: .topLeading)
                    .stroke(Color.yellow, lineWidth: 20)
                    .offset(x: 110, y: 350)
                    .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true))
                    .onAppear() {
                        self.animateTrimPath.toggle()
                    }
                }
                .rotationEffect(.degrees(rotaeInfinity ? 0 : -360))
                .scaleEffect(0.3, anchor: .center)
                .animation(Animation.easeInOut(duration: 1.5)
                .repeatForever(autoreverses: false))
                .onAppear(){
                    self.rotaeInfinity.toggle()
                }
            }
        }
    }
    
    struct Indicator_Previews: PreviewProvider {
        static var previews: some View {
            Indicator()
        }
    }
    
    

    0 讨论(0)
  • 2020-11-29 18:37

    I implemented the classic UIKit indicator using SwiftUI. See the activity indicator in action here

    struct ActivityIndicator: View {
      @State private var currentIndex: Int = 0
    
      func incrementIndex() {
        currentIndex += 1
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(50), execute: {
          self.incrementIndex()
        })
      }
    
      var body: some View {
        GeometryReader { (geometry: GeometryProxy) in
          ForEach(0..<12) { index in
            Group {
              Rectangle()
                .cornerRadius(geometry.size.width / 5)
                .frame(width: geometry.size.width / 8, height: geometry.size.height / 3)
                .offset(y: geometry.size.width / 2.25)
                .rotationEffect(.degrees(Double(-360 * index / 12)))
                .opacity(self.setOpacity(for: index))
            }.frame(width: geometry.size.width, height: geometry.size.height)
          }
        }
        .aspectRatio(1, contentMode: .fit)
        .onAppear {
          self.incrementIndex()
        }
      }
    
      func setOpacity(for index: Int) -> Double {
        let opacityOffset = Double((index + currentIndex - 1) % 11 ) / 12 * 0.9
        return 0.1 + opacityOffset
      }
    }
    
    struct ActivityIndicator_Previews: PreviewProvider {
      static var previews: some View {
        ActivityIndicator()
          .frame(width: 50, height: 50)
          .foregroundColor(.blue)
      }
    }
    
    
    0 讨论(0)
提交回复
热议问题