Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftUIで二重スクロール作ってみた / When I tried to make a d...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Elvis Shi
January 24, 2024
Programming
390
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
Elvis Shi
January 24, 2024
More Decks by Elvis Shi
See All by Elvis Shi
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
460
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
510
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
150
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
390
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
760
How did I build an Open-Source SwiftUI Toast Library
lovee
1
180
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
1.3k
Observation のあれこれ / A brief introduction about Observation
lovee
3
450
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
27
9k
Other Decks in Programming
See All in Programming
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
300
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
310
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.2k
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
3.3k
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
450
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
0
180
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
920
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
9
5.9k
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1.1k
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
230
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
720
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
670
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
280
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
sira's awesome portfolio website redesign presentation
elsirapls
0
310
ラッコキーワード サービス紹介資料
rakko
1
4.1M
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
RailsConf 2023
tenderlove
30
1.5k
Practical Orchestrator
shlominoach
191
11k
Are puppies a ranking factor?
jonoalderson
1
3.7k
Transcript
4XJGU6*Ͱ ೋॏεΫϩʔϧ࡞ͬͯΈͨ f o r Ϟ ό ν Ω
ʙ .PC JMF 5J Q T ڞ ༗ ձ ʙ
} var employedBy = "YUMEMI Inc." var job = "iOS
Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ ٱʑͷొஃ͗ͯ͢Կ͔ΕͯΔʂ """ final class Me: Developable, Talkable {
None
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN εΫϩʔϧͷॳظҐஔΛ ը૾ͷԼʹ
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
*UFN *UFN *UFN *UFN εΫϩʔϧͯ͠ ը૾ͷҐஔมΘΒͳ͍ ԼʹεΫϩʔϧʹͭΕͯ എܠͷෆಁ໌্͕͕Δ
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN ԼʹΦʔόʔεΫϩʔϧ͚ͨ࣌ͩ͠ ը૾ҰॹʹԼ͕Γ·͢
ϦϑϨογϡϓϩάϨε දࣔ͠·͢
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN എܠࣗମৗʹෆಈ
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN 0, ϋεϘλϯ͕λοϓ͞Ε·ͨ͠
࣮ͨͩͷը૾Ͱͳ͘ ϘλϯͰͨ͠ʂ
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN എܠ ϝΠϯϘλϯ
εΫϩʔϧϏϡʔ
struct BackgroundView: View { var body: some View { LinearGradient(
colors: [ .mint.opacity(0.5), .cyan, ], startPoint: .top, endPoint: .center ) .ignoresSafeArea() } }
struct PrimaryView: View { var body: some View { VStack
{ Button { } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } } .frame(maxHeight: .infinity, alignment: .top) } }
struct PrimaryView: View { @State private var showsDialog = false
var body: some View { VStack { Button { showsDialog = true } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } } .frame(maxHeight: .infinity, alignment: .top) .confirmationDialog("Message", isPresented: $showsDialog, actions: { Text("OK") }, message: { Text("ϋεϘλϯ͕λοϓ͞Ε·ͨ͠") }) } } 0, ϋεϘλϯ͕λοϓ͞Ε·ͨ͠
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
*UFN *UFN *UFN *UFN *UFN *UFN struct OperationScrollView: View { var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) } .background(Color.black.opacity(0.8)) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
struct OperationScrollView: View { var topPadding: CGFloat var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(0.8)) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN struct OperationScrollView: View { @State var componentsOffset: CGFloat var topPadding: CGFloat private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity )) } } ͯ͞ ͜ͷcomponentsOffsetΛ Ͳ͏ͬͯऔΔ͔
public struct PositionReader: ViewModifier { struct PositionKey: PreferenceKey { static
var defaultValue: CGFloat = 0 static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value += nextValue() } } var coordinateSpace: CoordinateSpace var position: KeyPath<CGRect, CGFloat> @Binding var value: CGFloat public func body(content: Content) -> some View { content .background( GeometryReader { geometry in Color.clear .preference(key: PositionKey.self, value: geometry.frame(in: coordinateSpace)[keyPath: position]) } ) .onPreferenceChange(PositionKey.self, perform: { newValue in value = newValue }) } } public extension View { func reading(_ keyPath: KeyPath<CGRect, CGFloat>, in coordinateSpace: CoordinateSpace, andAssignTo value: Binding<CGFloat>) -> some View { self.modifier(PositionReader(coordinateSpace: coordinateSpace, position: keyPath, value: value)) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN struct OperationScrollView: View { @State var componentsOffset: CGFloat var topPadding: CGFloat @Namespace private var scrollView: Namespace.ID private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) .reading(\.minY, in: .named(scrollView), andAssignTo: $componentsOffset) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity )) .coordinateSpace(.named(scrollView)) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN struct OperationScrollView:
View { @State var componentsOffset: CGFloat var topPadding: CGFloat @Namespace private var scrollView: Namespace.ID private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) .reading(\.minY, in: .named(scrollView), andAssignTo: $componentsOffset) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity)) .coordinateSpace(.named(scrollView)) .refreshable { reload() } } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN ԼʹΦʔόʔεΫϩʔϧ͚ͨ࣌ͩ͠ ը૾ҰॹʹԼ͕Γ·͢
struct PrimaryView: View { var topSpacing: CGFloat @State private var
showsDialog = false var body: some View { VStack { Spacer() .frame(height: max(0, topSpacing)) Button { showsDialog = true } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } } .frame(maxHeight: .infinity, alignment: .top) .confirmationDialog("Message", isPresented: $showsDialog, actions: { Text("OK") }, message: { Text("ϋεϘλϯ͕λοϓ͞Ε·ͨ͠") }) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN εΫϩʔϧͷॳظҐஔΛ ը૾ͷԼʹ
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
struct OperationScrollView: View { @State var componentsOffset: CGFloat var topPadding: CGFloat @Namespace private var scrollView: Namespace.ID private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) .reading(\.minY, in: .named(scrollView), andAssignTo: $componentsOffset) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity)) .coordinateSpace(.named(scrollView)) .refreshable { reload() } } } ͯ͞ࠓͷ ͜ͷtopPaddingΛ Ͳ͏ͬͯऔΔ͔
struct PrimaryView: View { @Binding var baseImageHeight: CGFloat var topSpacing:
CGFloat @State private var showsDialog = false var body: some View { VStack { Spacer() .frame(height: max(0, topSpacing)) Button { showsDialog = true } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } .reading(\.height, in: .local, andAssignTo: $baseImageHeight) } .frame(maxHeight: .infinity, alignment: .top) .confirmationDialog("Message", isPresented: $showsDialog, actions: { Text("OK") }, message: { Text("Did tap house button") }) } }
struct PrimaryView: View { @Binding var baseImageHeight: CGFloat var topSpacing:
CGFloat @State private var showsDialog = false var body: some View { VStack { Spacer() .frame(height: max(0, topSpacing)) Button { showsDialog = true } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } .reading(\.height, in: .local, andAssignTo: $baseImageHeight) } .frame(maxHeight: .infinity, alignment: .top) .confirmationDialog("Message", isPresented: $showsDialog, actions: { Text("OK") }, message: { Text("Did tap house button") }) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
struct OperationScrollView: View { @Binding var componentsOffset: CGFloat var topPadding: CGFloat @Namespace private var scrollView: Namespace.ID private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) .reading(\.minY, in: .named(scrollView), andAssignTo: $componentsOffset) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity)) .coordinateSpace(.named(scrollView)) .refreshable { reload() } } }
struct BackgroundView: View { var body: some View { LinearGradient(
colors: [ .mint.opacity(0.5), .cyan, ], startPoint: .top, endPoint: .center ) .ignoresSafeArea() } }
struct PrimaryView: View { @Binding var baseImageHeight: CGFloat var topSpacing:
CGFloat @State private var showsDialog = false var body: some View { VStack { Spacer() .frame(height: max(0, topSpacing)) Button { showsDialog = true } label: { Image(systemName: "house.lodge.fill") .resizable() .scaledToFit() } .reading(\.height, in: .local, andAssignTo: $baseImageHeight) } .frame(maxHeight: .infinity, alignment: .top) .confirmationDialog("Message", isPresented: $showsDialog, actions: { Text("OK") }, message: { Text("Did tap house button") }) } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
struct OperationScrollView: View { @Binding var componentsOffset: CGFloat var topPadding: CGFloat @Namespace private var scrollView: Namespace.ID private var scrollViewOpacity: CGFloat { max(min((-componentsOffset-10) / topPadding, 0.8), 0) } var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.flexible(minimum: 150, maximum: 300)), GridItem(.flexible(minimum: 150, maximum: 300))], content: { ForEach(0 ..< 20) { i in Text("Item: \(i)") .frame(maxWidth: .infinity) .frame(height: 100) .background(Color.blue) } }) .padding(.horizontal) .reading(\.minY, in: .named(scrollView), andAssignTo: $componentsOffset) } .safeAreaPadding(.top, topPadding) .background(Color.black.opacity(scrollViewOpacity)) .coordinateSpace(.named(scrollView)) .refreshable { reload() } } }
*UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN *UFN
struct ContentView: View { @State private var baseImageHeight: CGFloat = 0 @State private var componentsOffset: CGFloat = 0 var body: some View { PrimaryView( baseImageHeight: $baseImageHeight, topSpacing: componentsOffset - baseImageHeight ) .overlay { OperationScrollView( componentsOffset: $componentsOffset, topPadding: baseImageHeight ) } .background { BackgroundView() } }
%FNP
͜ͷ࣮ʹͪΐͬͱ͋Γ·͢! w ݱ࣌ͰSFGSFTIBCMFʹBTZODॲཧΛೖΕΒΕͳ͍ w ಠࣗͰ3FGSFTI"DUJPOʹରԠ͢Ε͍͍ w IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPO TXJGUVJSFGSFTIBDUJPO
͜ͷ࣮ʹͪΐͬͱ͋Γ·͢! w TBGF"SFB1BEEJOHʹෆ߹͕͋Δ w άϧάϧͷҐஔ͕Լ͕͍ͬͯΔ w ্ʹεΫϩʔϧͨ͠ࡍ4BGF"SFBൣғͰεΫϩʔ ϧϏϡʔʹ͋ΔϘλϯ͕λοϓͰ͖ͳ͍ w ΘΓʹεΫϩʔϧϏϡʔͷҰ൪্ʹ4QBDFSΛೖΕ
Δํ๏ߟ͑ΒΕΔ w ͦͷ߹εΫϩʔϧϏϡʔͷԼͷ1SJNBSZ7JFXͷ Ϙλϯ͕λοϓͰ͖ͳ͍
4XJGU6*໘ͤ͐͘
4XJGU6*໘ͤ͐͘ ୭͔ॿ͚͍ͯͩ͘͞ʂ