首页 > 解决方案 > 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")
                                                }
                                            }
                                        })
        }
    }
}

标签: swiftuiclosuresstate

解决方案


提供的代码是不可测试的,所以只是一个想法:

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)
}

推荐阅读