繁体   English   中英

SwiftUI animation 不会在更新绑定时发生

[英]SwiftUI animation not occurring on update of Binding

我正在尝试创建一个在绑定值非零时出现的“弹出视图”。

在此处输入图像描述

我有一个PopupCard class 它是通用的Content和一些Value ......

struct PopupCard<Content: View, Value>: View {
    @Binding private var data: Value?
    private let content: (Value) -> Content

    init(data: Binding<Value?>, @ViewBuilder content: @escaping (Value) -> Content) {
        self._data = data
        self.content = content
    }

    var body: some View {
        Group {
            if data != nil {
                HStack {
                    self.content(self.data!)
                        .padding()
                    Spacer()
                }
                .frame(width: UIScreen.main.bounds.width - 40)
                    .background(
                            Rectangle()
                                .fill(Color.yellow)
                                .shadow(color: Color.black.opacity(0.2), radius: 5, y: 0)
                    )
                    .offset(y: self.data == nil ? -100 : 0)
                    .animation(.default)
            }
        }
    }
}

我还在View上创建了以下扩展...

extension View {
    func popup<Content: View, Value>(data: Binding<Value?>, @ViewBuilder content: @escaping (Value) -> Content) -> some View {
        return ZStack {
            self
            VStack {
                Spacer()
                PopupCard(data: data, content: content)
            }
        }
    }
}

除了 animation 之外,这一切都很好。 该卡按预期出现和消失,但没有 animation。 这在以下预览中得到了演示……

struct PopupCard_Previews: PreviewProvider {
    struct PreviewView: View {
        @State var name: String? = "Hello"
        var body: some View {
            TabView {
                Button("Toggle", action: {
                    self.name = self.name == nil ? "Hello" : nil

                    })
                    .popup(data: $name) { string in
                        Text(string)
                }
            }
        }
    }

    static var previews: some View {
        PreviewView()
    }
}

我如何让这个动画进/出?

这是固定的变体(如果我正确理解了意图)。 使用 Xcode 11.4 / iOS 13.4 测试

演示

struct PopupCard<Content: View, Value>: View {
    @Binding private var data: Value?
    private let content: (Value) -> Content

    init(data: Binding<Value?>, @ViewBuilder content: @escaping (Value) -> Content) {
        self._data = data
        self.content = content
    }

    var body: some View {
        Group {
            HStack {
                if data != nil {
                    self.content(self.data!).padding()
                } else {
                    Text("")
                }
                Spacer()
            }
            .frame(width: UIScreen.main.bounds.width - 40)
                .background(
                        Rectangle()
                            .fill(Color.yellow)
                            .shadow(color: Color.black.opacity(0.2), radius: 5, y: 0)
                )
                .compositingGroup()
                .offset(y: self.data == nil ? 100 : 0)
                .animation(.default)
        }
    }
}

暂无
暂无

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

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