ios - 修复 SwiftUI 表单中奇怪的 DatePicker 动画行为

标签 ios swift swiftui ios-animations swiftui-form

我在 SwiftUI 表单中使用 DatePickers 获得了一些奇怪的动画行为。一张图片值一千字,所以我确信一个视频值一百万字:https://imgur.com/a/UHXqXOh
我试图让日期选择器在表单中展开然后折叠,就像在 Calendar.app 中创建新事件时的行为一样
发生在我身上的是:

  • Section中的任何扩展项目(除了最后一个)都会正常打开,但是当它关​​闭时,扩展的部分会向下滑动并淡出,而不是向上滑动和淡出。
  • 该部分中的最后一项可以正确滑动,但根本不会褪色。它只是在过渡开始/结束时出现然后消失

  • 这些行为仅在表单中某处存在非 DatePicker 元素(例如文本、 slider )时才会发生(不必在该特定部分中)
    这是我的内容 View :
    struct ContentView: View {
        @State var date = Date()
        @State var isDateShown = false
        var body: some View {
                Form {
                    Section(header: Text("Title")) {
                        DatePicker("Test", selection:$date)
                        DatePicker("Test", selection:$date)
                        Text("Pick a date").onTapGesture {
                            withAnimation {
                                self.isDateShown.toggle()
                            }
                           
                        }
                        if(isDateShown) {
                            DatePicker("", selection: $date).datePickerStyle(WheelDatePickerStyle()).labelsHidden()
                        }
                        
                    }
                    Section(header: Text("hello")) {
                        Text("test")
                    }
            }
            
        }
    }
    
    乐于提供其他所需的东西

    最佳答案

    以下是 iOS <14 的两种可能的解决方法:1) 简单的一种是完全禁用动画,2) 复杂的一种是通过注入(inject)自定义动画修饰符来缓解不正确的动画
    使用 Xcode 11.4/iOS 13.4 测试
    1)简单的解决方案 - 换行 DatePicker进入容器并将动画设置为零
    demo1

    VStack {
        DatePicker("Test", selection:$date).id(2)
    }.animation(nil)
    
    2) 复杂解决方案 - 抢 DatePicker使用 a) View 偏好阅读器更改框架 ViewHeightKey b) 使用 AnimatingCellHeight 显式地为该帧设置动画从我的其他解决方案。
    demo2
    struct TestDatePickersInForm: View {
        @State var date = Date()
        @State var isDateShown = false
        @State private var height = CGFloat.zero
        var body: some View {
                Form {
                    Section(header: Text("Title")) {
                        // demo of complex solution
                        VStack {
                            DatePicker("Test", selection:$date).id(1)
                                .background(GeometryReader {
                                    Color.clear.preference(key: ViewHeightKey.self,
                                        value: $0.frame(in: .local).size.height) })
                        }
                        .onPreferenceChange(ViewHeightKey.self) { self.height = $0 }
                        .modifier(AnimatingCellHeight(height: height))
                        .animation(.default)
    
                        // demo of simple solution
                        VStack {
                            DatePicker("Test", selection:$date).id(2)
                        }.animation(nil)
    
                        Text("Pick a date").onTapGesture {
                            withAnimation {
                                self.isDateShown.toggle()
                            }
    
                        }
                        if(isDateShown) {
                            DatePicker("", selection: $date).datePickerStyle(WheelDatePickerStyle()).labelsHidden().id(3)
                        }
    
                    }
                    Section(header: Text("hello")) {
                        Text("test")
                    }
            }
    
        }
    }
    

    关于ios - 修复 SwiftUI 表单中奇怪的 DatePicker 动画行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62570238/

    相关文章:

    ios - NSForegroundAttributeName 不工作。文本填充呈现透明

    swiftui - 如何在 Apple Watch 的扩展委托(delegate)中访问 SwiftUI 内容 View ?

    swift - SwiftUI 中的自定义部分初始值设定项

    iOS cancel (void)——执行并关闭ViewController

    iOS:如何在 Constants.h 中转换字符串中的类型?

    ios - 如何在 Objective-C 中的上一个高度和下一个高度之间留出一些空间来制作随机高度?

    ios - 继续到另一个 Storyboard

    swift - 使用 ImageMask Swift 填充图像

    swiftui - .previewLayout(PreviewLayout.fixed(width :_, height:_)) 在 Xcode 14 中不起作用

    ios - iOS 中应用内购买的服务器端验证最佳实践