ios - 过渡动画在 iOS 16 中不起作用,但在 iOS 15 中起作用

标签 ios swift animation swiftui ios16

我有一个带有自定义图 TableView (不是Swift Charts)的SwiftUI Form。长按切换到不同类型的图表。这些图表使用 .transition(.slide) 修饰符。在 iOS 15 中,这些在长按时会按预期进行转换,但在 iOS 16 中它们不会。

持久状态属性(枚举):

@AppStorage("chartType") var chartType: ChartType = .chartA

body 属性的 Form 部分:

Form {
    
    // Other sections
    
    Section {
        switch chartType {
            case .chartA:
                ChartViewA()
                    .transition(.slide)
            case .chartB:
                ChartViewB()
                    .transition(.slide)
    }
        .onLongPressGesture {
            if chartType == .chartA {
                withAnimation {
                    summaryChartType = .chartB
                }
            } else {
                withAnimation {
                    summaryChartType = .chartA
                }
            }
        }

不幸的是,添加像 .animation(.spring(), value: chartType) 这样的动画修饰符没有任何区别。

如果您能提供建议,说明为什么这可能在 iOS 15 中有效但在 iOS 16 中无效,以及我可以做些什么来恢复动画,我将不胜感激。

最佳答案

在 iOS 16 中,@AppStorage 变量和动画似乎存在问题。这是一种可能的解决方法。使用 @State var 制作动画,并使用 .onChange() 将其保存到 @AppStorage 变量:

enum ChartType: String {
    case chartA, chartB
}

struct ChartViewA: View {
    var body: some View {
        Color.red
    }
}

struct ChartViewB: View {
    var body: some View {
        Color.blue
    }
}

struct ContentView: View {
    @AppStorage("chartType") var chartTypeAS: ChartType = .chartA
    @State private var chartType: ChartType = .chartA
    
    init() {
        // load initial value from persistent storage
        _chartType = State(initialValue: chartTypeAS)
    }
    
    var body: some View {

        Form {
            
            // Other sections
            
            Section {
                VStack {
                    switch chartType {
                    case .chartA:
                        ChartViewA()
                            .transition(.slide)
                    case .chartB:
                        ChartViewB()
                            .transition(.slide)
                    }
                }
                .onLongPressGesture {
                    if chartType == .chartA {
                        withAnimation {
                            chartType = .chartB
                        }
                    } else {
                        withAnimation {
                            chartType = .chartA
                        }
                    }
                }
            }
            .onChange(of: chartType) { value in
                // persist chart type
                chartTypeAS = value
            }
        }
    }
}

使用运行 iOS 16 的 iPhone 14 模拟器在 Xcode 14.0 中测试。

或者,您可以手动执行保存到 UserDefaults 或从中恢复。

关于ios - 过渡动画在 iOS 16 中不起作用,但在 iOS 15 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73710154/

相关文章:

ios - 无法在 Facebook 分享中添加标题和描述

animation - 为什么这个 webkit 关键帧不是有效的 .less?

ios - 请帮助我的动画不工作 ios

arrays - 使用未解析的标识符 'WeekOne'

swift - “找不到构建输入文件”Swift 4.2、Xcode 10.0

android - RecyclerView ItemClick 过渡

ios - 删除额外的底部填充 UILabel

iOS 仅将一项服务宣传为外设 BTLE

ios - Flutter iOS 电子邮件文本字段内容类型

iOS:UIView 动画与 CABasic/CAKeyframe 动画的性能对比