简体   繁体   English

带有 Swift UI 的圆形进度条

[英]Circular Progress Bar with Swift UI

I am creating a countdown timer app with Swift UI.我正在使用 Swift UI 创建倒数计时器应用程序。 I tried to add a circular progress bar in it but its appearance never changes.我试图在其中添加一个圆形进度条,但它的外观从未改变。

I prepared 2 files.我准备了2个文件。 One of them is coded about countdown procedure and the other file is coded about UI.其中一个是关于倒计时程序的编码,另一个文件是关于 UI 的编码。 I make the data linking with the keys @ObservableObject, @Public for the procedure code and @ObservedObject for the UI code.我使用键@ObservableObject、@Public 为过程代码和@ObservedObject 为UI 代码建立数据链接。

A number which is set "counter" as variable is started to countdown 1 per second.将“计数器”设置为变量的数字开始每秒倒数 1。 I confirmed it works by printing the number in console of Xcode.我通过在 Xcode 的控制台中打印数字来确认它有效。

The number is down but progress bar does not change and finally the bar disappears once the count number is 0.数字下降但进度条没有改变,最后当计数为 0 时进度条消失。

Prodedual Code产品代码

import SwiftUI

class CountDownTimer: ObservableObject {

    @Published var counter: CGFloat = 10

    let interval = 1.0

    var timer: Timer?

    func start() {
        timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true, block: { _ in
            self.counter -= 1
            print(self.counter)

            if self.counter <= 0 {
            self.timer?.invalidate()
            self.timer = nil
          }
        })
    }

    func stop() {
        self.timer?.invalidate()
    }
}



UI Code用户界面代码

struct PresentationView: View {

    @ObservedObject var countDownTimer = CountDownTimer()

    var body: some View {
        NavigationView {
            VStack {

                Spacer()

                VStack {
                    ZStack {
                        Text(String(format: "%.0f", countDownTimer.counter))
                            .font(Font.largeTitle.monospacedDigit())
                            .fontWeight(.light)
                            .padding()

                        Circle()
                            .stroke(Color(.systemIndigo), style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .bevel))
                            .aspectRatio(contentMode: .fit)
                            .padding()

                        Circle().trim(from: 0, to: countDownTimer.counter)
                            .stroke(Color(.systemTeal), style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .bevel))
                            .aspectRatio(contentMode: .fit)
                            .rotationEffect(Angle(degrees: -90))
                            .padding()
                    }

                    Spacer()

                    // スタートボタンとストップボタン
                    HStack {
                        Button(action: {self.countDownTimer.stop()}) {
                            Text("STOP")
                                .fontWeight(.light)
                                .foregroundColor(.white)
                        }.frame(maxWidth: 75, maxHeight: 75)
                            .padding()
                            .background(Color(.systemIndigo))
                            .cornerRadius(100)
                            .padding()

                        Button(action: {self.countDownTimer.start()}) {
                            Text("START")
                                .fontWeight(.light)
                                .foregroundColor(.white)
                        }.frame(maxWidth: 75, maxHeight: 75)
                            .padding()
                            .background(Color(.systemTeal))
                            .cornerRadius(100)
                            .padding()

                    }

                }
            }
        }
    }
}

Please help me if you know how to fix this.如果您知道如何解决此问题,请帮助我。 My English might be broken because it's my second language.我的英语可能会被打破,因为它是我的第二语言。

Thank you.谢谢你。

You might need to divide endFraction by 10 ,您可能需要将endFraction除以10

Circle().trim(from: 0, to: countDownTimer.counter / 10) // Dividing by 10
      .stroke(Color(.systemTeal), style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .bevel))
      .aspectRatio(contentMode: .fit)
      .rotationEffect(Angle(degrees: -90))
      .padding()

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM