0

我尝试根据编辑模式更改视图状态,何时编辑隐藏视图,当它不编辑时显示视图,当我使用更改并打印编辑模式值时它的工作但它在使用视图时不起作用.

struct TaskStateMark_Row: View {
    
    @ObservedObject var task: Task
    @Environment(\.editMode) private var editMode
    
    var body: some View {
        Group {
        // Show and hide the view according to edit mode state
            if let editMode = editMode?.wrappedValue {
                if editMode == .inactive {
                    taskState
                        .onTapGesture(perform: onTapAction)
                }
            }
        }
    }
    
    
    
    private var taskState: some View {
        Group {
            if task.isCompleted {
                completedState
            } else {
                incompletedState
            }
        }
        .frame(width: 44, height: 44)
    }
    
    private var incompletedState: some View {
        ZStack{
            fillCircle
            circle
        }
    }
    
    private var circle: some View {
        Image(systemName: "circle")
            .font(.system(size: 24))
            .foregroundColor(task.wrappedPriority.color)
    }
    
    private var fillCircle: some View {
        Image(systemName: "circle.fill")
            .font(.system(size: 24))
            .foregroundColor(task.wrappedPriority.color.opacity(0.15))
    }
    
    private var completedState: some View {
        Image(systemName: "checkmark.circle.fill")
            .symbolRenderingMode(.palette)
            .foregroundStyle(.white, task.wrappedPriority.color)
            .font(.system(size: 24))
    }    
    
}
4

1 回答 1

0

我在以下链接的开发人员文档中找到了答案

https://developer.apple.com/documentation/swiftui/editmode

编码

@Environment(\.editMode) private var editMode
@State private var name = "Maria Ruiz"

var body: some View {
    Form {
        if editMode?.wrappedValue.isEditing == true {
            TextField("Name", text: $name)
        } else {
            Text(name)
        }
    }
    .animation(nil, value: editMode?.wrappedValue)
    .toolbar { // Assumes embedding this view in a NavigationView.
        EditButton()
    }
}
于 2022-02-18T07:23:15.523 回答