直接看代码案例快速入手SwiftUI
本文介绍Alert,ActionSheet,ProgressView。
🎉下载GitHub仓库,直接体验
Alert
struct AlertContentView: View {
@State private var showAlert = false
@State private var selectedItem: String? = nil
var body: some View {
VStack {
// Button("Show alert") {
// showAlert = true
// }
// .alert(isPresented: $showAlert) {
// Alert(title: Text("Alert"), message: Text("This is a message"), dismissButton: .default(Text("OK")))
// }
Button("Show alert") {
showAlert = true
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Alert"),
message: Text("This is a alert with two buttons"),
primaryButton: .default(Text("Confirm"), action: {
print("Click confirm")
}),
secondaryButton: .cancel(Text("Cancel")))
}
}
}
}
struct AlertContentView_Previews: PreviewProvider {
static var previews: some View {
AlertContentView()
}
}
ActionSheet
struct ActionSheetContentView: View {
@State private var showActionSheet = false
var body: some View {
// Button("Show ActionSheet") {
// showActionSheet = true
// }
// .actionSheet(isPresented: $showActionSheet) {
// ActionSheet(title: Text("Action"),
// message: Text("This is a ActionSheet example"),
// buttons: [.default(Text("Item 1")), .default(Text("Item 2")), .cancel()])
// }
// Button("Show ActionSheet") {
// showActionSheet = true
// }
// .actionSheet(isPresented: $showActionSheet) {
// ActionSheet(title: Text("Action"),
// message: Text("Thi is a ActionSheet with action"),
// buttons: [
// .default(Text("Button 1"), action: {
// print("Button 1 clicked")
// }),
// .default(Text("Button 2"), action: {
// print("Button 2 clicked")
// }),
// .cancel()
// ])
// }
Button("Show ActionSheet") {
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor.red
showActionSheet = true
}
.actionSheet(isPresented: $showActionSheet) {
ActionSheet(title: Text("Action"),
message: Text("This is a ActionSheet with custom style"),
buttons: [
.default(Text("Item 1"), action: {
print("Item 1 executed")
}),
.default(Text("Item 2"), action: {
print("Item 2 executed")
}),
.cancel(Text("Cancel"))
])
}
}
}
struct ActionSheetContentView_Previews: PreviewProvider {
static var previews: some View {
ActionSheetContentView()
}
}
ProgresView
struct ProgressViewContentView: View {
@State private var progress = 0.0
let workoutDateRange = Date() ... Date().addingTimeInterval(5 * 60)
let progresses: [Double] = [0.2, 0.5, 0.7, 0.9]
var body: some View {
VStack {
ProgressView(value: 0.1, total: 1.0)
ProgressView(value: 0.3, total: 1.0)
.progressViewStyle(LinearProgressViewStyle(tint: .red))
ProgressView(value: 1, total: 1.0)
.progressViewStyle(CircularProgressViewStyle(tint: .green))
ProgressView()
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
ProgressView("Loading...", value: 0.6, total: 1.0)
ProgressView(value: progress, total: 1.0)
if #available(iOS 16.0, *) {
ProgressView(timerInterval: workoutDateRange) {
Text("Workout")
}
} else {
// Fallback on earlier versions
}
Button("Add progress") {
if progress < 1.0 {
progress += 0.1
}
}
.padding(.top)
ForEach(progresses, id: \.self) { progress in
ProgressView(value: progress)
.padding(.bottom)
}
}
}
}
struct ProgressViewContentView_Previews: PreviewProvider {
static var previews: some View {
ProgressViewContentView()
}
}
网友评论