【问题标题】:Move inside list element position in SwiftUI在 SwiftUI 中移动列表元素位置
【发布时间】:2020-10-07 17:53:04
【问题描述】:

当我点击另一个列表元素时,我正在尝试移动一个列表元素的位置

当我点击那会发生什么

我需要视图向上移动并保持这种状态,而无需用户移动它

        VStack{
            VStack{
                Button(action: {
                    self.iconeView.toggle()
                }) {
                    ZStack{
                        self.icone.renderingMode(.original)
                            .frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
                    }
                }

            }//Photo
                .padding(.bottom, 54)
                .padding(.top, 42)

            List {

                VStack(alignment: .leading){
                    Text("Nome do remédio")
                }//Medicine Name
                .padding(.bottom, 10)

                VStack(alignment: .leading){
                    Text("Quantidade de remédio")
                }// Quantity
                    .padding(.bottom, 50)

                VStack(alignment: .leading){
                    Text("Horario Inicial")

                }//Start time

            }//List


        }//Screen

【问题讨论】:

    标签: swiftui swiftui-list


    【解决方案1】:

    由于您已经预定义了元素数量,因此在这里使用ScrollView 而不是List 更合适。所以解决方案可以如下(伪代码)

    更新完整的演示代码。使用 Xcode 11.4 / iOS 13.4 测试

    struct DemoMoveToTopView: View {
        @State private var showMedicalName = true
        @State private var showQuantaty = true
    
        var body: some View {
            ScrollView {
                VStack {
                if self.showMedicalName {
                    VStack(alignment: .leading){
                        Text("Nome do remédio")
                    }//Medicine Name
                        .frame(width: 300, height: 100)
                        .border(Color.red).background(Color.yellow)
                        .padding(.bottom, 10)
                        .transition(.opacity)
                }
    
                if self.showQuantaty {
                    VStack(alignment: .leading){
                        Text("Quantidade de remédio")
                    }// Quantity
                        .frame(width: 300, height: 100)
                        .border(Color.red).background(Color.yellow)
                        .padding(.bottom, 50)
                        .transition(.opacity)
                }
    
                VStack(alignment: .leading){
                    Text("Horario Inicial")
    
                }//Start time
                    .frame(width: 300, height: 100)
                        .border(Color.red).background(Color.white)
                    .onTapGesture {
                        self.showMedicalName.toggle()      // << hide/show above
                        self.showQuantaty.toggle()         // << hide/show above
    
                        // .. do other needed
                }
    
                }.animation(.default)
            }//ScrollView
        }
    }
    

    【讨论】:

    • 谢谢,但是切换到 ScrollView 我仍然无法在激活 Tapgesture 时将列表元素位置移动到顶部
    • @EduardoAirton,我相信在调整提供的想法时发生了一些错误。查看更新的完整功能测试演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2023-02-10
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多