You need to enable JavaScript to run this app. SwiftUI开发TikTok克隆:点击缩略图始终播放首个视频问题求解
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI开发TikTok克隆:点击缩略图始终播放首个视频问题求解

问题根源

你当前遇到的问题核心是VideoScrollView里的视频数据源完全没用到传入的posts参数:

  • 声明postPlayer时,你用的是PostArrayObject().dataArray.map(...),这里的PostArrayObject()是新建的默认实例,永远会生成你硬编码的6个默认视频
  • 同时currentPost默认值是空字符串,和tag(item.id)匹配不上时,TabView会默认选中第一个元素
    所以不管你点击哪个缩略图,加载的都是默认的第一个视频。

修复方案

场景1:点进视频后仅查看当前选中的单个视频,不需要上下滑动刷其他视频

仅需修改VideoScrollView里的postPlayer声明逻辑,将数据源替换为传入的posts即可:

// 删除原来的postPlayer声明代码,替换为以下逻辑
@State var postPlayer: [PostModel] = []

var body: some View {
    GeometryReader { proxy in
        // 原有布局逻辑不变
    }
    .onAppear {
        // 用传入的posts生成带播放器的视频模型
        postPlayer = posts.dataArray.map { item -> PostModel in
            let url = Bundle.main.path(forResource: item.url, ofType: "MP4") ?? ""
            let player = AVPlayer(url: URL(fileURLWithPath: url))
            return PostModel(
                postID: item.postID,
                userID: item.userID,
                dateCreated: item.dateCreated,
                favoritedByUser: item.favoritedByUser,
                url: item.url,
                caption: item.caption,
                favoritesCount: item.favoritesCount,
                sharesCount: item.sharesCount,
                commentsCount: item.commentsCount,
                productPrice: item.productPrice,
                songDescription: item.songDescription,
                username: item.username,
                modelProfilePic: item.modelProfilePic,
                player: player
            )
        }
        // 直接选中当前唯一的视频
        currentPost = postPlayer.first?.id ?? ""
    }
    .ignoresSafeArea(.all, edges: .top)
    .background(Color.black.ignoresSafeArea())
}

场景2:点进视频后可上下滑动刷全部视频,定位到当前选中的视频

需要同时修改BrowseView传参逻辑和VideoScrollView接收逻辑:

第一步:修改BrowseView的NavigationLink传参

将原来传单个post的数组改为传完整的视频数组,同时传入选中的post的id:

// 替换原来的NavigationLink代码
NavigationLink(
    destination: VideoScrollView(currentTab: $currentTab, posts: posts, initialPostId: post.id, scrollEnabled: true),
    label: {
        VideoThumbnailView(post: post, currentUsersPost: false).foregroundColor(.white)
    }
)

第二步:修改VideoScrollView的接收逻辑

struct VideoScrollView: View {
    @Binding var currentTab: String
    @ObservedObject var posts: PostArrayObject
    // 新增接收选中的帖子ID
    private let initialPostId: String
    @State var currentPost = ""
    @State var postPlayer: [PostModel] = []
    
    // 新增自定义构造器
    init(currentTab: Binding<String>, posts: PostArrayObject, initialPostId: String, scrollEnabled: Bool) {
        self._currentTab = currentTab
        self.posts = posts
        self.initialPostId = initialPostId
    }
    
    var body: some View {
        GeometryReader { proxy in
            let size = proxy.size
            // 原有TabView布局逻辑不变
            TabView(selection: $currentPost) {
                ForEach($postPlayer) { $item in
                    PostPlayer(post: $item, currentPost: $currentPost, currentTab: $currentTab)
                        .frame(width: size.width)
                        .rotationEffect(.init(degrees: -90))
                        .ignoresSafeArea(.all, edges: .top)
                        .tag(item.id)
                }
            }
            .rotationEffect(.init(degrees: 90))
            .frame(width: size.height)
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .frame(width: size.width)
        }
        .onAppear {
            // 用传入的完整数组生成带播放器的模型
            postPlayer = posts.dataArray.map { item -> PostModel in
                let url = Bundle.main.path(forResource: item.url, ofType: "MP4") ?? ""
                let player = AVPlayer(url: URL(fileURLWithPath: url))
                return PostModel(
                    postID: item.postID,
                    userID: item.userID,
                    dateCreated: item.dateCreated,
                    favoritedByUser: item.favoritedByUser,
                    url: item.url,
                    caption: item.caption,
                    favoritesCount: item.favoritesCount,
                    sharesCount: item.sharesCount,
                    commentsCount: item.commentsCount,
                    productPrice: item.productPrice,
                    songDescription: item.songDescription,
                    username: item.username,
                    modelProfilePic: item.modelProfilePic,
                    player: player
                )
            }
            // 自动定位到选中的视频
            currentPost = initialPostId
        }
        .ignoresSafeArea(.all, edges: .top)
        .background(Color.black.ignoresSafeArea())
    }
}

内容的提问来源于stack exchange,提问作者ihatecoding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 01:48:03