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におけるPreferenceの基礎概念
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Kota Minato
May 21, 2025
Technology
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SwiftUIにおけるPreferenceの基礎概念
Kota Minato
May 21, 2025
More Decks by Kota Minato
See All by Kota Minato
design docで開発効率を向上させる
kotaminato
0
140
Other Decks in Technology
See All in Technology
Oracle AI Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
6
1.5k
LLMにもCAP定理があるという話
harukasakihara
0
380
AIネイティブな開発のサプライチェーンリスク対策 〜激動の開発現場でリスクに立ち向かう〜【ZennFes】
cscengineer
PRO
2
130
200個のGitHubリポジトリを横断調査したかった
icck
0
130
Bucharest Tech Week 2026 - Reinventing testing practices in the AI era
edeandrea
PRO
1
160
新しいUbuntu/GNOMEが使いたいからXからWaylandへ移行頑張ってるの巻 2026-06-20
nobutomurata
0
120
AAIFに入ってみた ~内から見えるコミュニティ動向~
sato4
0
240
エラーバジェットのアラートのタイミングを考える.pdf
kairim0
0
150
2026 TECHFRESH 畢業分享會 - 開發日常大解密!從領域驅動到企業級上線
line_developers_tw
PRO
0
1.1k
LayerX コーポレートエンジニアリング室におけるサプライチェーンセキュリティへの取り組み / Supply Chain Security at LayerX Corporate Engineering
yuyatakeyama
2
430
NAB Show 2026 動画技術関連レポート / NAB Show 2026 Report
cyberagentdevelopers
PRO
0
200
LayerXにおけるセキュリティ管理の現在地と次の一手
tosho
0
200
Featured
See All Featured
Building the Perfect Custom Keyboard
takai
2
790
Chasing Engaging Ingredients in Design
codingconduct
0
220
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
580
Abbi's Birthday
coloredviolet
2
8.1k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
WENDY [Excerpt]
tessaabrams
11
38k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
280
Transcript
© 2025 Wantedly, Inc. SwiftUIにおける Preferenceの基礎概念 Mobile勉強会#20 ウォンテッドリー×チームラボ×Sansan May.21 2025
- kota minato
© 2025 Wantedly, Inc. 自己紹介
© 2025 Wantedly, Inc. 話すこと 01 Preferenceとはなにか ? 02 Binding
vs Environment vs Preference 03 Wantedlyでの活用事例
© 2025 Wantedly, Inc. 話すこと 01 Preferenceとはなにか ? 02 Binding
vs Environment vs Preference 03 Wantedlyでの活用事例
© 2025 Wantedly, Inc. Preferenceとはなにか? ずばり、SwiftUIにおいて、子Viewから親Viewへ値を伝達する仕組み 通常データは親Viewから子Viewへ流れるがPreferenceを用いると子Viewから親Viewへの一方通行 のデータの受け渡しが可能になる
© 2025 Wantedly, Inc. どういったときにPreferenceを使うのか? レイアウト情報やサイズ情報を親Viewに 伝える必要がある場合に有用 もっと具体的には ? ScrollView内の子Viewの位置を取得し、親Viewで制御し
たい場合など
© 2025 Wantedly, Inc. 話すこと 01 Preferenceとはなにか ? 02 Binding
vs Environment vs Preference 03 Wantedlyでの活用事例
© 2025 Wantedly, Inc. Binding vs Environment vs Preference Binding、Environment、Preferenceを比較してみる
@Environmentは上位View→下位Viewへの一方向の値注入 (広い範囲で共通の値を共有 ) @Bindingは親View→子Viewへの双方向の値注入 (データの保有者は親 View) Preferenceは子View→親Viewへの一方向の値注入 データの矢印の向きが異なります!
© 2025 Wantedly, Inc. 話すこと 01 Preferenceとはなにか ? 02 Binding
vs Environment vs Preference 03 Wantedlyでの活用事例
© 2025 Wantedly, Inc. Wantedlyでの活用事例 下スクロールを検知でボタンを非表示にしてほしい!
© 2025 Wantedly, Inc. Wantedlyでの活用事例 実装方針は子Viewのスクロールを検知して親 Viewでボタン を非表示にする処理を行おう! でもどうやって子Viewから親Viewに通知しよう☹ 画面構成としては親Viewの中に子Viewが入っている!
© 2025 Wantedly, Inc. Wantedlyでの活用事例 Preference
© 2025 Wantedly, Inc. どうやって実装するのか PreferencesKeyに準拠したstructを作成 • PreferencesKeyに準拠するために必要なのはdefaultValue とreduce。 •
defaultValue = 何も入れなかったときの値 • reduce = 子Viewが入れた値を親Viewが受け取るときの変 換処理を書く 今回はOffSetを常に更新して取得したかったので Valueに対してnextValueを加算していま す。
© 2025 Wantedly, Inc. どうやって実装するのか • .preference(key: ScrollOffset.self, value: 値
) 子Viewから親Viewへ値を流す ※ 画面に影響を与えないようにColor.Clearを使用して値を取得します
© 2025 Wantedly, Inc. どうやって実装するのか 親Viewで値を受け取る ボタン非表示アニメーションを追加 (詳細は割愛) • .onPreferenceChange(ScrollOffset.self
) {}
© 2025 Wantedly, Inc. Wantedlyでの活用事例 うまくできた🎉
© 2025 Wantedly, Inc. まとめ • PreferenceはViewの子から親に値を渡す仕組み • PreferenceKeyを実装することによって子 View側で値を入れ親側で
.onPreferenceChengeで受け取 ることができる
© 2025 Wantedly, Inc. ご清聴ありがとうございまし た!