我有一个三个级别的比赛,每个级别都是5分(总共15分).我想用代表每个级别的三个同心圆或环来显示用户的进度.圆环一开始是空的,只有一个轮廓,当用户收集一个点时,五分之一的圆被填充了一种 colored颜色 ,直到整个圆被填充,在这一点上,这个过程重复到下一个关卡.我有下面的代码,但出于某种原因,当我在模拟器中运行它时,圆圈已经填满了.我如何才能让他们在用户获得积分时逐渐填满(一次填满五分之一)?

import SwiftUI

struct ContentView: View {
    @State private var progressLevel1: CGFloat = 0.0
    @State private var progressLevel2: CGFloat = 0.0
    @State private var progressLevel3: CGFloat = 0.0

    var body: some View {
        VStack {
            Spacer()
            
            ZStack {
                Circle()
                    .stroke(Color.green, lineWidth: 20)
                    .frame(width: 150, height: 150)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel1)
                            .stroke(Color.green, lineWidth: 20)
                            .frame(width: 150, height: 150)
                            .rotationEffect(.degrees(-90))
                    )
                
                Circle()
                    .stroke(Color.blue, lineWidth: 20)
                    .frame(width: 200, height: 200)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel2)
                            .stroke(Color.blue, lineWidth: 20)
                            .frame(width: 200, height: 200)
                            .rotationEffect(.degrees(-90))
                    )
                
                Circle()
                    .stroke(Color.purple, lineWidth: 20)
                    .frame(width: 250, height: 250)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel3)
                            .stroke(Color.purple, lineWidth: 20)
                            .frame(width: 250, height: 250)
                            .rotationEffect(.degrees(-90))
                    )
            }
            
            Spacer()
            
            Button("Collect Point") {
                withAnimation {
                    if progressLevel1 < 1.0 {
                        progressLevel1 += 0.2
                    } else if progressLevel2 < 1.0 {
                        progressLevel2 += 0.2
                    } else if progressLevel3 < 1.0 {
                        progressLevel3 += 0.2
                    }
                }
            }
            .padding()
        }
        .onAppear {
            // Set initial values to 0 to start with empty circles
            progressLevel1 = 0.0
            progressLevel2 = 0.0
            progressLevel3 = 0.0
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

如果您希望最初显示一个模糊的轮廓,请使用此方法:

 Circle().stroke(Color.green.opacity(0.1), lineWidth: 20)

对于其他 colored颜色 (不是叠加中的圆)也是如此.调整opacity到你喜欢的.

编辑-1:

以下是我的完整测试代码.它通过逐渐填充 同心圆或环代表每个级别,具有不同的 colored颜色 .

在MacOS 14.2上,使用Xcode 15.1,在真实的ios17设备(不是预览版)和MacCatalyst上进行测试.

struct ContentView: View {
    @State private var progressLevel1: CGFloat = 0.0
    @State private var progressLevel2: CGFloat = 0.0
    @State private var progressLevel3: CGFloat = 0.0

    var body: some View {
        VStack {
            Spacer()
            
            ZStack {
                Circle()
                    .stroke(Color.green.opacity(0.1), lineWidth: 20) // <-- here
                    .frame(width: 150, height: 150)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel1)
                            .stroke(Color.green, lineWidth: 20)
                            .frame(width: 150, height: 150)
                            .rotationEffect(.degrees(-90))
                    )
                
                Circle()
                    .stroke(Color.blue.opacity(0.1), lineWidth: 20) // <-- here
                    .frame(width: 200, height: 200)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel2)
                            .stroke(Color.blue, lineWidth: 20)
                            .frame(width: 200, height: 200)
                            .rotationEffect(.degrees(-90))
                    )
                
                Circle()
                    .stroke(Color.purple.opacity(0.1), lineWidth: 20) // <-- here
                    .frame(width: 250, height: 250)
                    .overlay(
                        Circle()
                            .trim(from: 0.0, to: progressLevel3)
                            .stroke(Color.purple, lineWidth: 20)
                            .frame(width: 250, height: 250)
                            .rotationEffect(.degrees(-90))
                    )
            }
            
            Spacer()
            
            Button("Collect Point") {
                withAnimation {
                    if progressLevel1 < 1.0 {
                        progressLevel1 += 0.2
                    } else if progressLevel2 < 1.0 {
                        progressLevel2 += 0.2
                    } else if progressLevel3 < 1.0 {
                        progressLevel3 += 0.2
                    }
                }
            }
            .padding()
        }
        .onAppear {
            // Set initial values to 0 to start with empty circles
            progressLevel1 = 0.0
            progressLevel2 = 0.0
            progressLevel3 = 0.0
        }
    }
}

Swift相关问答推荐

数据不会被ARC删除在swift'

在visionOS RealityView中使用.GenerateText时,未显示Reality Composer Promaterial 纹理

并发访问SWIFT中数组的非重叠子范围

swift:只要框架名称中有一个带有框架名称的公共实体,就指定框架中公共 struct 的路径

Swift 数组拆分成重叠的块

如何将视图添加到多行文本视图的末尾?

Swiftui 无法从核心数据中获取数据

为什么 String.contains 在我导入 Foundation 时表现不同?

有没有更快的方法来循环浏览 macOS 上已安装的应用程序?

快速递归:函数与闭包

如何更新 UserDefault 中的特定值

无法分配给属性:absoluteString是一个只能获取的属性

用于 Swift 编码的 iOS 应用程序的 Ffmpeg

.onTapGesture 不适用于 Stepper,仅适用于其文本

在视图中设置所有变量

仅在 Swift 中创建 Setter

使 struct 可散列?

Swift 自定义字体 Xcode

UIButton 标题左对齐问题 (iOS/Swift)

如何在 SwiftUI 中的一个视图上显示两个alert ?