繁体   English   中英

过渡 animation 在 iOS16 中不工作但在 iOS15 中工作

[英]Transition animation not working in iOS16 but was working in iOS15

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

持久的 state 属性(枚举):

@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 修饰符,如.animation(.spring(), value: chartType)没有任何区别。

我将不胜感激为什么这可能在 iOS 15 中有效但在 iOS 16 中有效,以及我可以做些什么来恢复 animation 在这里。

在 iOS 16 中, @AppStorage vars 和 animation 似乎存在问题。 这是一种可能的解决方法。 对 animation 使用@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
            }
        }
    }
}

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

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

暂无
暂无

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

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