Chr*_*ris 6 animation ios swift swiftui ios16
我有一个SwiftUI Form自定义图表视图(不是Swift Charts)。长按可切换到不同类型的图表。这些图表使用.transition(.slide)修饰符。在 iOS 15 中,长按时这些会按预期进行转换,但在 iOS 16 中却不会。
持久化状态属性(枚举):
@AppStorage("chartType") var chartType: ChartType = .chartA
Run Code Online (Sandbox Code Playgroud)
Form财产部分body:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,添加动画修改器之类的.animation(.spring(), value: chartType)没有什么区别。
如果您能告诉我为什么这在 iOS 15 中有效但在 iOS 16 中无效,以及我可以采取哪些措施来恢复动画,我将不胜感激。
vac*_*ama 12
在 iOS 16 中,变量和动画似乎存在问题@AppStorage。这是一种可能的解决方法。使用@Statevar 进行动画,并将其保存到@AppStorage变量中.onChange():
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用运行 iOS 16 的 iPhone 14 模拟器在 Xcode 14.0 中进行测试。
或者,您可以手动执行保存/恢复UserDefaults。
| 归档时间: |
|
| 查看次数: |
1980 次 |
| 最近记录: |