swift 我该怎么做,tint()和,background()会影响我的组件吗?

bihw5rsg  于 2023-04-28  发布在  Swift
关注(0)|答案(1)|浏览(106)

我想知道我怎么能。tint()会影响我的组件。

public var body: some View {
        ZStack {
            Circle()
                // it should use the `.background()` color from outside
                .stroke(.background, lineWidth: 15)
            Circle()
                .trim(from: 0, to: CGFloat(min(progress, 1.0)))
                // it should use the `.tint()` color from outside
                .stroke(Color.accentColor, style: StrokeStyle(lineWidth: 15, lineCap: .round))
                .rotationEffect(.degrees(-90))
        }
    }

我想实现与这个apple组件相同的语法。使用颜色。accentColor似乎只在我调用.accentColor()时起作用,但在我调用.tint()时保持蓝色。我也不知道如何使用任何一种backgroundColor

ProgressView(value: 0.5)
   .tint(.red)
   .background(.green)
wztqucjr

wztqucjr1#

ShapeStyle上有一个tint属性:https://developer.apple.com/documentation/swiftui/shapestyle/tint
它可以这样使用:

struct ContentView: View {
    var body: some View {
        ChildView()
            .tint(Color.green)
    }
}

struct ChildView: View {
    var body: some View {
        Rectangle().fill(.tint)
    }
}

应用于您的示例:

Circle()
    .trim(from: 0, to: CGFloat(min(progress, 1.0)))
    .stroke(.tint, style: StrokeStyle(lineWidth: 15, lineCap: .round))
    .rotationEffect(.degrees(-90))

相关问题