You need to enable JavaScript to run this app. SwiftUI 1.0(适配iOS13)实现每页3图动态分页图片画廊
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI 1.0(适配iOS13)实现每页3图动态分页图片画廊

适配iOS 13(SwiftUI 1.0)的3图单页翻页式图片画廊实现

核心设计思路

  • 全程使用SwiftUI 1.0原生API开发,不引入iOS 14及以上才支持的特性,完美覆盖iOS 13系统版本
  • 依托TabView原生横向滑动能力实现左滑翻页交互,避免自定义手势带来的冲突问题
  • 分页圆点数量完全由图片总数动态推导,无需硬编码适配场景

动态分页计算规则

  • 单页固定槽位展示3张图片,最后一页自动承接不足3张的剩余图片,槽位不足时用空白占位保证单页总宽度与其他页一致,避免滑动时布局跳动
  • 总页数(即分页圆点总数量)计算公式为:Int(ceil(Double(总图片数) / 3.0))
  • 规则验证:总图片数7-9张时计算值为3个圆点;总图片数14张时计算值为5个圆点,完全匹配需求场景

分模块代码实现

图片分组工具

将全量图片按每3张一组拆分为独立分页内容,自动处理最后一页不足3张的边界情况:

func splitImagesToPages(_ images: [UIImage]) -> [[UIImage]] {
    stride(from: 0, to: images.count, by: 3).map { startIndex in
        let endIndex = min(startIndex + 3, images.count)
        return Array(images[startIndex..<endIndex])
    }
}

单页图片布局视图

实现固定宽度的单页内容,不足3张的位置自动留空,保证滑动时布局稳定:

struct GalleryPageView: View {
    let pageImages: [UIImage]
    private let slotCount = 3
    private let imageWidth = (UIScreen.main.bounds.width - 48) / 3 // 减去左右32边距+两个8pt间距
    var body: some View {
        HStack(spacing: 8) {
            ForEach(0..<slotCount, id: \.self) { index in
                if index < pageImages.count {
                    Image(uiImage: pageImages[index])
                        .resizable()
                        .scaledToFill()
                        .frame(width: imageWidth, height: 200)
                        .clipped()
                        .cornerRadius(8)
                } else {
                    Color.clear
                        .frame(width: imageWidth, height: 200)
                }
            }
        }
        .padding(.horizontal, 16)
    }
}

主画廊视图(含翻页+动态分页指示器)

依托iOS 13支持的TabView原生横向滑动能力实现左滑翻页,自定义绘制适配分页数量的指示器:

struct PagedGalleryView: View {
    let allImages: [UIImage]
    @State private var currentPage = 0
    // 预计算分页内容与总页数
    private var pageGroups: [[UIImage]] {
        splitImagesToPages(allImages)
    }
    private var totalPageCount: Int {
        pageGroups.count
    }
    var body: some View {
        VStack(spacing: 16) {
            // 横向翻页区域
            TabView(selection: $currentPage) {
                ForEach(0..<pageGroups.count, id: \.self) { pageIndex in
                    GalleryPageView(pageImages: pageGroups[pageIndex])
                        .tag(pageIndex)
                }
            }
            .frame(height: 220)
            
            // 动态分页指示器
            HStack(spacing: 8) {
                ForEach(0..<totalPageCount, id: \.self) { dotIndex in
                    Circle()
                        .fill(dotIndex == currentPage ? Color.black : Color.gray.opacity(0.3))
                        .frame(width: dotIndex == currentPage ? 8 : 6, height: dotIndex == currentPage ? 8 : 6)
                        .animation(.easeInOut(duration: 0.2))
                }
            }
        }
    }
}

适配注意事项

  • 禁止使用iOS 14才引入的LazyHGrid/LazyVGrid实现布局,否则会在iOS 13设备上触发运行时崩溃
  • 分页指示器的圆点数量直接读取分组后的页数组长度,不需要额外写判断逻辑,自动适配任意总图片数的场景
  • 无需额外叠加自定义DragGesture实现翻页,TabView原生的横向滑动已完全覆盖左滑切换下一组图片的需求,不存在手势冲突问题

内容的提问来源于stack exchange,提问作者Josh Byanec McFergan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.05 11:06:10