20
17

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】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が一般に開示した情報」に該当してると思うので大丈夫だとは思いますが、
もしアウトだったらコメントで教えて欲しいです。

20
17
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
20
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?