swiftui - Swiftui:如何重置布尔状态?
问题描述
当我点击编辑时,它将显示一个删除按钮(减号图标)。当点击删除按钮时,它将显示橙色删除选项(如 gif 所示)。现在,当点击完成按钮时,我正在尝试将状态重置回原点(从橙色按钮回到视频名称和长度)。我正在尝试一些选项,例如关闭,但没有太多。任何帮助将非常感激!
我的孩子观看视频
struct Video: View {
var videoImage : String
var title : String
var duaration : Int
@Binding var deleteActivated : Bool
var body: some View {
HStack {
Image(videoImage)
...
if deleteActivated {
Button(action: {
}) {
ZStack {
Rectangle()
.foregroundColor(.orange)
.cornerRadius(radius: 10, corners: [.topRight, .bottomRight])
Text("Delete")
.foregroundColor(.white)
}
}
} else {
VStack(alignment: .leading){
....
我的父母查看视频目录
struct VideosDirectory: View {
@State var videos:[DraftVideos] = [
DraftVideos(isSelected: true,title: "Superman workout", duration: 5, imageURL: "test"),
DraftVideos(isSelected: true,title: "Ironman workout", duration: 15, imageURL: "test1"),
DraftVideos(isSelected: true,title: "Ohman workout and long name", duration: 522, imageURL: "test2")
]
init() {
self._deleteActivated = State(initialValue: Array(repeating: false, count: videos.count))
}
@State private var deleteActivated: [Bool] = []
@State private var show = false
// @State private var editing = false
var body: some View {
// VStack {
NavigationView {
ScrollView(.vertical) {
VStack {
ForEach(videos.indices, id: \.self) { i in
HStack {
if self.show {
Button(action: {
withAnimation {
self.deleteActivated[i].toggle()
}
}) {
Image(systemName: "minus.circle.fill")
...
}
}
Video(videoImage: videos[i].imageURL, title: videos[i].title, duaration: videos[i].duration, deleteActivated: $deleteActivated[i])
}
}
}
.animation(.spring())
}
.navigationBarItems(trailing:
HStack {
Button(action: {
self.show.toggle()
}) {
if self.show {
Text("Done")
} else {
Text("Edit")
}
}
})
}
}
}
解决方案
提供的代码是不可测试的,所以只是一个想法:
Button(action: {
self.deleteActivated = Array(repeating: false, count: videos.count)
self.show.toggle()
}) {
或几乎相同,但与
}
.animation(.spring())
.onChange(of: self.show) { _ in
// most probably condition is not needed here, but is up to you
self.deleteActivated = Array(repeating: false, count: videos.count)
}
推荐阅读
- html - 我的下拉列表中的标签在图像上不可点击
- node.js - express js只接受5个请求
- django - 模板代码在不同的应用程序模板中不起作用
- pdf - 通过注册表更改 Acrobat Reader 中的“标记前确认”设置
- google-visualization - Google 可视化表 - 绘制两次,显示不同的列
- linux - 从 html 格式化到输出
- delphi - 更改样式时如何保持组件的颜色
- ios - 在 iOS 应用程序中启动 TestFlight(深度链接)
- java - 我应该如何通过 toString 以格式化的方式打印 ArrayList 中的所有元素?
- powershell - PowerShell 等待 Enter 键