4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】横スライド式画像ビュワーを作成する

Posted at

はじめに

インスタのような横スライド式の画像ビュワーを作っていきます。
RPReplay_Final1662379581_AdobeExpress.gif

実装

PhotosView
import SwiftUI

struct PhotosView<T: View>: View {
    private let width: CGFloat
    private let height: CGFloat
    private let content: () -> T

    init(width: CGFloat, height: CGFloat, @ViewBuilder content: @escaping () -> T) {
        self.width = width
        self.height = height
        self.content = content
    }
    var body: some View {
        TabView {
            content()
        }
        .tabViewStyle(.page)
        .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .interactive))
        .frame(width: width, height: height)
        .background(Color(UIColor.secondarySystemFill))
    }
}

使い方

ContentView
import SwiftUI

struct ContentView: View {
    var body: some View {
        PhotosView(width: 350, height: 250) {
            ForEach(0..<3) { index in
                Image("\(index)")
                    .resizable()
                    .scaledToFit()
            }
        }
        .cornerRadius(10)
    }
}

完成形

Simulator Screen Recording - iPhone 12 - 2022-09-05 at 21.25.36.gif

おわり

サンプル画像は最近流行りのStable Diffusionで作ってみました

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?