Upgrade to Pro — share decks privately, control downloads, hide ads and more …

uhooiさん発ランダムタスクにチャレンジしよう!

 uhooiさん発ランダムタスクにチャレンジしよう!

集まれSwift好き!Swift愛好会 vol.80 @ DeNA
https://love-swift.connpass.com/event/311304/

Avatar for 野瀬田 裕樹

野瀬田 裕樹

August 10, 2026

More Decks by 野瀬田 裕樹

Other Decks in Programming

Transcript

  1. お題1の解答編 - コンテンツのy座標に連動させる private struct ScrollContentViewMinYPreferenceKey: PreferenceKey { static var

    defaultValue: [CGFloat] = [0] static func reduce(value: inout [CGFloat], nextValue: () -> [CGFloat]) { value.append(contentsOf: nextValue()) } }
  2. お題1の解答編 - コンテンツのy座標に連動させる ScrollView { content().overlay { GeometryReader { proxy

    in Color.clear.preference( key: ScrollContentViewMinYPreferenceKey.self, value: [proxy.frame(in: .global).minY] ) } } }.onPreferenceChange(ScrollContentViewMinYPreferenceKey.self) { value in scrollContentViewMinY = value[0] }
  3. お題1の解答編 - 連動したpositionより上だけ色を変更 .background { VStack(spacing: 0) { topBackgroundColor.frame(height: max(scrollContentViewMinY,

    0)) bottomBackgroundColor.frame(height: scrollViewHeight - scrollContentViewMinY) }.ignoresSafeArea() }.overlay { GeometryReader { proxy in Color.clear.onAppear { scrollViewHeight = proxy.frame(in: .global).height } }.ignoresSafeArea() }
  4. お題1の解答編 - 結果 • これで良い感じになった! • https://x.com/ynoseda/status/1737120393960558947?s=20 • 補足 ◦

    別に基準値はminYじゃなくてもmaxYでもなんでもいいです ▪ コンテンツの裏のどこかのpositionで切り替わればいいので
  5. お題2の解答編 - コード • https://gist.github.com/yuukiw00w/0972b4fd33a08fe774228ebaa592558e • 実装は↑にあるが、汚すぎるので参考までに • https://x.com/ynoseda/status/1741515297054744844?s=20 •

    補足 ◦ パフォーマンス低そうな気がするので実運用に耐えられるかは微妙 ◦ 階層は違うけど裏にいるViewの色を判定するAPIとか欲しい