繁体   English   中英

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

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

我正在尝试解决 iOS 15 的警告: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead. 在出现在屏幕顶部的视图中,看起来像推送通知警报(或所谓的小吃店),但是当我在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))
    }
}

我尝试使用一个单独的 boolean state var,当self.show设置为 true 时切换但无济于事,我如何使用easeInOut(duration:) animation 使用 iOS 15 的 8823708899542 为这个小吃店视图的外观设置动画?

将 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 !!
    }
}

用 Xcode 13.2 / iOS 15.2 测试

暂无
暂无

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

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