簡體   English   中英

SwiftUI 意外 animation 上出現

[英]SwiftUI unexpected animation on appear

我創建了自定義TabBar

一切正常,直到我將選項卡的一個視圖嵌入到NavigationView中,結果 - 從左上角到右上角的View的外觀 animation(帶有文本和黃色的屏幕):

在此處輸入圖像描述

tabBar 是如何完成的:基本上View的主體是使用GeometryReader VStack / Zstack創建的:

var body: some View {
    GeometryReader { geometry in
        let height = geometry.size.height
        let width = geometry.size.width
        let bottomSafeAreaInset = geometry.safeAreaInsets.bottom
        let topSafeAreaInset = geometry.safeAreaInsets.top
        let verticalSafeAreaInset = bottomSafeAreaInset + topSafeAreaInset
        
        VStack(spacing: 0) {
            // content
            mainContentBody
                .frame(width: width, height: height - heightOfTabBar)
                .zIndex(0)
            
             // some calculation ...

            // tabBar
            Spacer(minLength: 0)
            BottomBar(barButtonItems: buttons)
                .frame(width: width, height: tabBarHeightWithOffset)
                .background(Color.gray)
                .offset(y: isMenuShown ? tabBarHeightWithOffset : 0)
                .edgesIgnoringSafeArea(.all)
                .opacity(isMenuShown ? 0 : 1)
                .tabContainerAnimation() // simple wrapper for animation with duration
            
            //... other view in ZStack
            
            // button
            ZStack {
                overlayButton
            }
            .offset(y: -initialButtonOffset + additionalOffsetForButton)
            .tabContainerAnimation(delay: 0.25)
        }
    }
}

在 tab1 上查看代碼:

struct Tab1View: View {
    
    var body: some View {
            NavigationView {
                VStack {
                    Text("sdsd")
                    Color.orange
                }
            }
    }
}

如果我刪除NavigationView這個效果也會被刪除。 所以我的問題是 - 為什么我有這個意想不到的 animation? 做錯了什么?

這是修復(使用 Xcode 12.1 / iOS 14.1 測試)

struct Tab1View: View {
    
    var body: some View {
        GeometryReader { g in
            NavigationView {
                VStack {
                    Text("sdsd")
                    Color.orange
                }
            }
            .animation(.none)     // << this one !!
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM