Overview
概要
Use a progress view to show that a task is making progress towards completion. A progress view can show both determinate (percentage complete) and indeterminate (progressing or not) types of progress.
Create a determinate progress view by initializing a ProgressView
with a binding to a numeric value that indicates the progress, and a total
value that represents completion of the task. By default, the progress is 0.0
and the total is 1.0
.
The example below uses the state property progress
to show progress in a determinate ProgressView
. The progress view uses its default total of 1.0
, and because progress
starts with an initial value of 0.5
, the progress view begins half-complete. A “More” button below the progress view allows the user to increment the progress in 5% increments:
@State private var progress = 0.5
VStack {
ProgressView(value: progress)
Button("More", action: { progress += 0.05 })
}
To create an indeterminate progress view, use an initializer that doesn’t take a progress value:
var body: some View {
ProgressView()
}
Styling Progress Views
プログレスビューにスタイルをつける
You can customize the appearance and interaction of progress views by creating styles that conform to the ProgressViewStyle
protocol. To set a specific style for all progress view instances within a view, use the progressViewStyle(_:)
modifier. In the following example, a custom style adds a dark blue shadow to all progress views within the enclosing VStack
:
struct ShadowedProgressViews: View {
var body: some View {
VStack {
ProgressView(value: 0.25)
ProgressView(value: 0.75)
}
.progressViewStyle(DarkBlueShadowProgressViewStyle())
}
}
struct DarkBlueShadowProgressViewStyle: ProgressViewStyle {
func makeBody(configuration: Configuration) -> some View {
ProgressView(configuration)
.shadow(color: Color(red: 0, green: 0, blue: 0.6),
radius: 4.0, x: 1.0, y: 2.0)
}
}