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
Kota Minato
May 21, 2025
Technology
0
150
SwiftUIにおけるPreferenceの基礎概念
Kota Minato
May 21, 2025
Tweet
Share
Other Decks in Technology
See All in Technology
Oracle Cloud Infrastructure:2025年12月度サービス・アップデート
oracle4engineer
PRO
0
180
あの夜、私たちは「人間」に戻った。 ── 災害ユートピア、贈与、そしてアジャイルの再構築 / 20260108 Hiromitsu Akiba
shift_evolve
PRO
0
350
20251225_たのしい出張報告&IgniteRecap!
ponponmikankan
0
110
ソフトウェアエンジニアとAIエンジニアの役割分担についてのある事例
kworkdev
PRO
1
360
Oracle Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
1
820
Redshift認可、アップデートでどう変わった?
handy
1
120
テストセンター受験、オンライン受験、どっちなんだい?
yama3133
0
200
re:Invent2025 セッションレポ ~Spec-driven development with Kiro~
nrinetcom
PRO
2
170
投資戦略を量産せよ 2 - マケデコセミナー(2025/12/26)
gamella
0
590
Qiita Bash アドカレ LT #1
okaru
0
150
Data Hubグループ 紹介資料
sansan33
PRO
0
2.5k
RALGO : AIを組織に組み込む方法 -アルゴリズム中心組織設計- #RSGT2026 / RALGO: How to Integrate AI into an Organization – Algorithm-Centric Organizational Design
kyonmm
PRO
3
500
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
120
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.9k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.1k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
220
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
41
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Leo the Paperboy
mayatellez
0
1.3k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
65
Large-scale JavaScript Application Architecture
addyosmani
515
110k
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. ご清聴ありがとうございまし た!