LoginSignup
18
15

【SwiftUI】iOS17からScrollViewのページングが簡単になった!

Last updated at Posted at 2023-06-09

はじめに

iOS17からスクロールのページングが簡単にできるようになったみたいなので試してみます!

サンプルアプリ

縦ページング 横ページング
Simulator Screen Recording - iPhone 14 Pro - 2023-06-09 at 21.06.56.gif Simulator Screen Recording - iPhone 14 Pro - 2023-06-09 at 21.10.33.gif

実装

縦ページング

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical) {
            VStack {
                ForEach(0..<50, id: \.self) { index in
                    Text("ページ: \(index)")
                        .frame(maxWidth: .infinity)
                        .frame(height: UIScreen.main.bounds.height - 125)
                        .background(Color.cyan)
                        .cornerRadius(15)
                        .padding(20)
                }
            }
            .scrollTargetLayout()
        }
        .scrollTargetBehavior(.paging)
    }
}

横ページング

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(0..<50, id: \.self) { index in
                    Text("ページ: \(index)")
                        .frame(width: UIScreen.main.bounds.width - 45)
                        .frame(maxHeight: .infinity)
                        .background(Color.cyan)
                        .cornerRadius(15)
                        .padding(20)
                }
            }
            .scrollTargetLayout()
        }
        .scrollTargetBehavior(.paging)
    }
}

ドキュメント

おわり

めっちゃ簡単にできるようになってて感動です!

スクリーンショット 2023-06-06 17.23.08.png
「Appleが一般に開示した情報」に該当してると思うので大丈夫だとは思いますが、
もしアウトだったらコメントで教えて欲しいです。

18
15
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
18
15