0

使用 Swift5.3.2、iOS14.4.1、XCode12.4、

如以下代码所示,我在 SwiftUI 的 Page-Mode 中使用了一个相当复杂的 TabView。

即使用 iOS14 显示页面的新可能性:

.tabViewStyle(PageTabViewStyle())

一切正常。

除非,如果我将 iPhone 从纵向旋转到横向,TabView 会断开连接并将 selectedTab 索引设置为 0(即,无论您滚动到哪里,旋转 iPhone 都会意外地重置为第 0 页)。

父视图本身处于复杂的视图层次结构中。并且在 TabView 显示(和滑动)期间更新 TabView 的父视图之一。这可能是 TabView 在旋转到横向时重新渲染的问题。

在 iPhone 旋转期间我能做些什么来保持 TabView-Page ?

这是代码:

import SwiftUI

struct PageViewiOS: View {
    
    var body: some View {
        
        ZStack {
            
            Color.black
            
            MediaTabView()
                
            CloseButtonView()
        }
    }
}

以及有问题的 MediaTabView:

struct MediaTabView: View {
    
    @EnvironmentObject var appStateService: AppStateService
    @EnvironmentObject var commService: CommunicationService

    @State private var tagID = ""
    @State private var selectedTab = 0
    @State private var uniqueSelected = 0
    @State private var IamInSwipingAction = false
    
    var body: some View {
        
        let myDragGesture = DragGesture(minimumDistance: 10)
            .onChanged { _ in
                IamInSwipingAction = true
                DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(5000)) {
                    IamInSwipingAction = false  // workaround: because onEnded does not work...
                }
            }
            .onEnded { _ in
                IamInSwipingAction = false
            }
        
        TabView(selection: self.$selectedTab) {
            if let list = appStateService.mediaViewModel.mediaList.first(where: { (list) -> Bool in
                switch appStateService.appState {
                case .content(let tagID):
                    return list.tagId == tagID
                default:
                    return false
                }
            }) {
                if list.paths.count > 0 {
                    ForEach(list.paths.indices, id: \.self) { index in
                        ZoomableScrollView {
                            if let url = URL(fileURLWithPath: list.paths[index]){
                                if url.containsImage {
                                    Image(uiImage: UIImage(contentsOfFile: url.path)!)
                                        .resizable()
                                        .scaledToFit()
                                } else if url.containsVideo {                                    
                                    CustomPlayerView(url: url)
                                } else {
                                    Text(LocalizedStringKey("MediaNotRecognizedKey"))
                                        .multilineTextAlignment(.center)
                                        .padding()
                                }
                            } else {
                                Text(LocalizedStringKey("MediaNotRecognizedKey"))
                                    .multilineTextAlignment(.center)
                                    .padding()
                            }
                        }
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.black)
                        .onAppear() {
                            if uniqueSelected != selectedTab {
                                uniqueSelected = selectedTab
                                if IamInSwipingAction && (commService.communicationRole == .moderatorMode) {
                                    commService.send(thCmd: THCmd(key: .swipeID, sender: "", content: URL(fileURLWithPath: list.paths[index]).lastPathComponent))
                                }
                            }
                        }
                    }
                } else {
                    Text(LocalizedStringKey("EmptyOrNoTrihowAlbumKey"))
                        .multilineTextAlignment(.center)
                        .padding()
                }
            } else {
                if Constants.TrihowAlbum.tagIdArrayTrihowAlbum.contains(tagID) {
                    Text(LocalizedStringKey("EmptyOrNoTrihowAlbumKey"))
                        .multilineTextAlignment(.center)
                        .padding()
                } else {
                    Text(LocalizedStringKey("TagNotRecognizedKey"))
                        .multilineTextAlignment(.center)
                        .padding()
                }
            }
        }
            .onAppear() {
                switch appStateService.appState {
                case .content(let tagID):
                    self.tagID = tagID
                default:
                    self.tagID = ""
                }
            }
            .tabViewStyle(PageTabViewStyle())
            .onTHComm_ReceiveCmd(service: commService) { (thCmd) in
                switch thCmd.key {
                case .swipeID:
                    if (commService.communicationRole == .moderatorMode) || (commService.communicationRole == .discoveryMode) {
                        selectTabFromCmdID(fileName: thCmd.content)
                    } else {
                        break
                    }
                default:
                    break
                }
            }
            .simultaneousGesture(myDragGesture)
    }
}

extension MediaTabView {
        
    private func selectTabFromCmdID(fileName: String) {
        
        if let list = appStateService.mediaViewModel.mediaList.first(where: { (list) -> Bool in
            return list.tagId == tagID
        }) {
            if list.paths.count > 0 {
                if let idx = list.paths.firstIndex(where: { (urlPath) -> Bool in
                    if let url = URL(string: urlPath) {
                        return url.lastPathComponent == fileName
                    } else { return false }
                }) {
                    selectedTab = idx
                }
            }
        }
    }
}
4

0 回答 0