【问题标题】:Circular Progress Bar with Swift UI带有 Swift UI 的圆形进度条
【发布时间】:2020-06-15 15:01:48
【问题描述】:

我正在使用 Swift UI 创建一个倒数计时器应用程序。我尝试在其中添加一个圆形进度条,但它的外观从未改变。

我准备了 2 个文件。其中一个是关于倒计时程序的编码,另一个文件是关于 UI 的编码。我使用键@ObservableObject、@Public 为过程代码和@ObservedObject 为UI 代码进行数据链接。

将“计数器”设置为变量的数字开始每秒倒数 1。我通过在 Xcode 的控制台中打印数字来确认它有效。

数字下降但进度条没有变化,最后当计数为0时进度条消失。

产品代码

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()
    }
}



用户界面代码

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()

                    }

                }
            }
        }
    }
}

如果您知道如何解决此问题,请帮助我。 我的英语可能会被打断,因为它是我的第二语言。

谢谢。

【问题讨论】:

    标签: ios swift xcode user-interface swiftui


    【解决方案1】:

    您可能需要将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()
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多