简体   繁体   English

如何使用 SwiftUI 列表中的自定义视图平滑扩展行单元格?

[英]How to expand a row cell smoothly with a custom view in SwiftUI List?

I am trying to expand a row cell (which is a custom view) on tap gesture.我正在尝试在点击手势上展开一个行单元格(这是一个自定义视图)。 The cell height has to be increased and a button is being moved in the expanded area.单元格高度必须增加,并且按钮正在扩展区域中移动。 But I get the following jumpy animation effect.但我得到以下跳跃的 animation 效果。 The pressed blue card shall remain steady but instead it jumps around.被按下的蓝牌应保持稳定,但它会跳来跳去。

在此处输入图像描述

Simple Code to reproduce this jumpy animation:重现这个跳跃的 animation 的简单代码:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
            List {
                Detail(isExpanded: false)
                Detail(isExpanded: false)
                Detail(isExpanded: false)
            }
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}



struct Detail: View {
    @State var isExpanded :Bool
    var body: some View {
        ZStack {
            ZStack {
                RoundedRectangle(cornerRadius: 8)
                .fill(Color(red: 0.0, green: 1.0, blue: 1.0, opacity: 1.0)).frame(height: 115)
                Text("Hello, World!")
            }.zIndex(3).frame(height: 115).contentShape(Rectangle()).onTapGesture {
                withAnimation {
                    self.isExpanded.toggle()
                }
            }
            Button(action: {
            }) {
                ZStack {
                    RoundedRectangle(cornerRadius: 50)
                        .fill(Color(red: 1.0, green: 0.0, blue: 1.0, opacity: 1.0)).frame(height: 70)
                        .cornerRadius(8)
                        .shadow(radius: 3)
                        Text("Test")
                }
            }.padding([.leading, .trailing], 12)
                .padding(.top, 6)
                .frame(height: 70)
                .buttonStyle(BorderlessButtonStyle())
                .offset(y: self.isExpanded ? 0 : 40)
                .disabled(!self.isExpanded)
        }.frame(height: self.isExpanded ? 120 : 200)
    }
}

I am grateful for any tips or hints on how to smoothe this out.我很感激任何关于如何解决这个问题的提示或提示。

Edit编辑

When clicked the hello world card should remain aligned at the top in the cell itself.单击时,hello world 卡片应在单元格本身的顶部保持对齐。 Like so:像这样: 在此处输入图像描述

Just use animatable modifier from this my solution只需使用我的解决方案中的可动画修改器

Tested with Xcode 11.4 / iOS 13.4使用 Xcode 11.4 / iOS 13.4 测试

演示

ZStack {
 // .. other your code here
}
.modifier(AnimatingCellHeight(height: self.isExpanded ? 120 : 200))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM