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
450
ゼロから始める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
730
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
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
1.3k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
7
4.4k
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
670
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
4.1k
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.1k
Oxlintのカスタムルールの現況
syumai
6
1.1k
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
550
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.6k
Oxcを導入して開発体験が向上した話
yug1224
4
310
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
2
640
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Design in an AI World
tapps
1
240
Deep Space Network (abreviated)
tonyrice
0
170
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Scaling GitHub
holman
464
140k
Utilizing Notion as your number one productivity tool
mfonobong
4
320
Ethics towards AI in product and experience design
skipperchong
2
310
Code Reviewing Like a Champion
maltzj
528
40k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
210
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
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*໘ͤ͐͘ ୭͔ॿ͚͍ͯͩ͘͞ʂ