はじめに
iOS17からスクロールのページングが簡単にできるようになったみたいなので試してみます!
SwiftUI's ScrollView became very powerful! It allows you to tune scroll targets and enable paging very easily.https://t.co/7ydUWE5sSa pic.twitter.com/jRG4pO9w9R
— Majid Jabrayilov (@mecid) June 8, 2023
サンプルアプリ
縦ページング | 横ページング |
---|---|
![]() |
![]() |
実装
縦ページング
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)
}
}
ドキュメント
おわり
めっちゃ簡単にできるようになってて感動です!
「Appleが一般に開示した情報」に該当してると思うので大丈夫だとは思いますが、
もしアウトだったらコメントで教えて欲しいです。