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
Elvis Shi
January 24, 2024
Programming
380
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
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
490
ゼロから始める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
750
How did I build an Open-Source SwiftUI Toast Library
lovee
1
170
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
440
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
27
9k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
370
Other Decks in Programming
See All in Programming
AIエージェントで 変わるAndroid開発環境
takahirom
2
550
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
140
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
140
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.3k
継続モナドとリアクティブプログラミング
yukikurage
3
500
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
210
フィードバックで育てるAI開発
kotaminato
1
110
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.8k
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
110
関数型プログラミングのメリットって何だろう?
wanko_it
0
170
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
150
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
150
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
180
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
760
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
220
We Have a Design System, Now What?
morganepeng
55
8.2k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Chasing Engaging Ingredients in Design
codingconduct
0
230
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.4k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
72
40k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
4 Signs Your Business is Dying
shpigford
187
22k
Skip the Path - Find Your Career Trail
mkilby
1
170
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*໘ͤ͐͘ ୭͔ॿ͚͍ͯͩ͘͞ʂ