简体   繁体   English

SwiftUI:“值”参数在 iOS 15 的新 animation 方法签名中未按预期工作

[英]SwiftUI: 'value' parameter not working as expected in iOS 15's new animation method signature

I'm trying to solve the iOS 15's warning: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.我正在尝试解决 iOS 15 的警告: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead. in a view that appears on top of screen that looks like push notification alert (or what so called snack bar), but the view loses animation effect when I use any Equatable value in the value parameter, the code for this view is as follows (you can copy & paste it as is to try it out):在出现在屏幕顶部的视图中,看起来像推送通知警报(或所谓的小吃店),但是当我在value参数中使用任何Equatable值时,该视图将失去 animation 效果,该视图的代码如下(您可以按原样复制并粘贴它来试用):

import Foundation
import SwiftUI

struct ContentView: View {
    @State private var showSnackBar = false
    
    var body: some View {
        
        ZStack {
            Color.gray
            
            Text("Show Me")
                .padding()
                .onTapGesture {
                    showSnackBar = true
                }
            
            EmptyView()
                .snackBar(title: "this is a test message", show: $showSnackBar)
                .offset(x: 0, y:  50)
        }
        .edgesIgnoringSafeArea(.all)
    }
}


struct SnackBarModifier: ViewModifier {
    var title: String
    @Binding var show: Bool
    @State var task: DispatchWorkItem?
    
    func body(content: Content) -> some View {
        
        ZStack {
            if self.show {
                VStack {
                    HStack {
                        
                        Text(title)
                        
                        Spacer()
                    }
                    .foregroundColor(.white)
                    .padding(12)
                    .background(Color.orange)
                    .cornerRadius(8)
                    
                    Spacer()
                }
                    .frame(maxWidth: UIScreen.main.bounds.width - 40)
                    .animation(.easeInOut(duration: 0.7), value: ...) //<---- this is where I should use the value in order to solve iOS 15 animation deprecation warning
                    .transition(AnyTransition.move(edge: .top)
                        .combined(with: .opacity))
                    
                    .onTapGesture {
                        withAnimation {
                            self.show = false
                        }
                }
                .onAppear {
                    self.task = DispatchWorkItem {
                        withAnimation {
                            self.show = false
                        }
                    }
                    // Auto dismiss after 2 seconds
                    DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: self.task!)
                }
                .onDisappear {
                    self.task?.cancel()
                }
            }
            
            content
        }
        
    }
}

extension View {
    func snackBar(title: String, show: Binding<Bool>) -> some View {
        self.modifier(SnackBarModifier(title: title, show: show))
    }
}

I tried to use a separate boolean state var that is toggled when the self.show is set to true but to no avail, how can I animate the appearance of this snack bar view using easeInOut(duration:) animation using iOS 15's animation style?我尝试使用一个单独的 boolean state var,当self.show设置为 true 时切换但无济于事,我如何使用easeInOut(duration:) animation 使用 iOS 15 的 8823708899542 为这个小吃店视图的外观设置动画?

Put animation modifier on top ZStack container, like将 animation 修饰符放在ZStack容器的顶部,例如

struct SnackBarModifier: ViewModifier {
    var title: String
    @Binding var show: Bool
    @State var task: DispatchWorkItem?

    func body(content: Content) -> some View {

        ZStack {
            if self.show {

                // other content ...
            }

            content
        }
        .animation(.easeInOut(duration: 0.7), value: show)  // << here !!
    }
}

Tested with Xcode 13.2 / iOS 15.2用 Xcode 13.2 / iOS 15.2 测试

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

相关问题 新的 SwiftUI/iOS 15.0 .animation 无法按预期工作 - New SwiftUI/iOS 15.0 .animation not working as intended iOS 15 SwiftUI 3 更改@State 值后选取器绑定不起作用 - iOS 15 SwiftUI 3 Picker binding is not working after changing @State value 过渡 animation 在 iOS16 中不工作但在 iOS15 中工作 - Transition animation not working in iOS16 but was working in iOS15 SwiftUI 在新的基础格式化程序中更改语言 iOS 15 - SwiftUI Change language in new Foundation Formatters iOS 15 导航栏隐藏在 SwiftUI iOS15 中不起作用 - Navigation Bar hide not working in SwiftUI iOS15 PHPickerViewController 的取消按钮在 iOS 15 中不起作用 - PHPickerViewController's Cancel Button is not working in iOS 15 iOS - 动画未停止,并且未按预期调用完成方法 - iOS - Animation was not stopped and completion method was not called as expected iOS 15:在 SwiftUI 中使用 UISheetPresentationController - iOS 15: using UISheetPresentationController in SwiftUI SwiftUI:iOS 15 崩溃无效参数不满足:pos&#39; *** 首先抛出调用堆栈: - SwiftUI : iOS 15 crash Invalid parameter not satisfying: pos' *** First throw call stack: SwiftUI LazyVGrid 转换 animation 不符合预期 - SwiftUI LazyVGrid transition animation not as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM