【问题标题】:SwiftUI transition is not working properlySwiftUI 转换无法正常工作
【发布时间】:2020-03-13 10:38:06
【问题描述】:

我正在尝试在视图出现时从底部边缘过渡到顶部。

 ZStack{
   VStack {
    .
    .//other components
    .
    if self.modalShown {
        HStack {
            GeometryReader { geometry in
                BottomSheetView(
                    isOpen: self.$modalShown,
                    maxHeight: geometry.size.height * 0.6
                ) {
                    ZStack(alignment: .bottom) {
                        FiltersView(hideControl: self.$modalShown)
                            .transition(.move(edge: .bottom))
                    }

                    .edgesIgnoringSafeArea(.bottom)
                }
            }
        }
        .accessibility(identifier: "modalFilterView")
    } 
}

问题是当这个视图出现时,它执行了一种 alpha 0 到 alpha 1 的转换。当视图被关闭时,它不会执行任何转换。

可以将这种不良行为与 zIndex 相关联吗?

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

    问题是转换修饰符放错了位置:

     ZStack{
       VStack {
        .
        .//other components
        .
        if self.modalShown {
            HStack {
                GeometryReader { geometry in
                    BottomSheetView(
                        isOpen: self.$modalShown,
                        maxHeight: geometry.size.height * 0.6
                    ) {
                        ZStack(alignment: .bottom) {
                            FiltersView(hideControl: self.$modalShown)
                        }
                        .edgesIgnoringSafeArea(.bottom)
                    }
                }
            }
            .transition(.move(edge: .bottom))
            .accessibility(identifier: "modalFilterView")
        } 
    }
    

    【讨论】: