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
CollectionViewの 新しいレイアウトの作り方
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ry-itto
November 02, 2019
Programming
78
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CollectionViewの 新しいレイアウトの作り方
ry-itto
November 02, 2019
More Decks by ry-itto
See All by ry-itto
決定版!?OSSアプリプロジェクトでのBeta版アプリ配布の方法「Xcode Cloud + TestFlight」
ryitto
0
400
CA.swift#14
ryitto
3
5.9k
swift-argument-parserで 簡単 CLI ツール作り
ryitto
1
170
Data Essentials in SwiftUI
ryitto
1
530
Composable Architecture
ryitto
0
810
Swift5.1 SwiftUI
ryitto
0
140
Other Decks in Programming
See All in Programming
Oxlintのカスタムルールの現況
syumai
6
1.1k
The NotImplementedError Problem in Ruby
koic
1
850
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
スマートグラスで並列バイブコーディング
hyshu
0
200
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
290
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
100
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
さぁV100、メモリをお食べ・・・
nilpe
0
150
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
370
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
210
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
The SEO Collaboration Effect
kristinabergwall1
1
490
GraphQLとの向き合い方2022年版
quramy
50
15k
Ethics towards AI in product and experience design
skipperchong
2
310
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Crafting Experiences
bethany
1
180
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
Un-Boring Meetings
codingconduct
0
320
Transcript
CollectionViewͷ ৽͍͠ϨΠΞτͷ࡞Γํ ձେֶֶ෦3ɹҏ౻྇
ࣗݾհ • ҏ౻྇ • ΧϐόϥѪ • iOSΤϯδχΞ • ͚ͭ໙େ͖
CollectionViewͱʁ “UICollectionView is a flexible, powerful tool to help you
achieve great user experiences in your applications.” –WWDC2018 A Tour of UICollectionView https://developer.apple.com/videos/play/wwdc2018/225/
͜Μͳը໘Λදݱ͢ΔͨΊʹ͏
iOS13Ͱ৽͘͠Ճ͞Εͨ CollectionViewपΓͷAPI • Compositional Layouts • DiffableDataSource
iOS13Ͱ৽͘͠Ճ͞Εͨ CollectionViewपΓͷAPI • Compositional Layouts • DiffableDataSource
Compositional Layouts • Item • Group • Section • Layout
ͷ4߲ͰCollectionViewͷϨΠΞτΛߏ͢Δ 88%$"EWBODFTJO$PMMFDUJPO7JFX-BZPVU TMJEF ˞ ˞ IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED
Compositional Layoutsͷ ੜ·Εͨཧ༝ʢલஔ͖ʣ • ࡢࠓͷΞϓϦͷUIෳࡶʹͳ͖͍ͬͯͯΔ • ैདྷͷFlowLayoutΛͬͨϨΠΞτߏͩͱ ͔ͳΓෳࡶͳίʔυʹͳͬͯ͠·͏ • ύϑΥʔϚϯε໘Λߟ͑ͳ͕ΒUIΛߏ͢Δඞཁ
͕͋ͬͨ
Compositional Layoutsͷ ࡾຊͷப • ComposableʢߏՄೳʣ γϯϓϧͳίʔυͰෳࡶͳUI͕࡞ΕΔ • Flexibleʢॊೈʣ ෳࡶͳUI͕࡞ΕΔ •
Fastʢૣ͍ʣ ύϑΥʔϚϯεʹྀ͠ͳͯ͘ྑ͍
σϞ, ίʔυ
αϯϓϧΞϓϦ ηΫγϣϯ͕ෳ͋ͬ ͯͦΕͧΕͷηΫγϣ ϯ͕10ݸ΄ͲͷཁૉΛ ͍࣋ͬͯΔ εΫϩʔϧ͢Δ
ैདྷͷFlowLayout Λ༻ͨ͠߹
࣮ํ େݩͷ $PMMFDUJPO7JFX ʢ5BCMF7JFXͰՄʣ େݩͷ $PMMFDUJPO7JFXͷηϧ ࢠͷ$PMMFDUJPO7JFX ࢠͷ$PMMFDUJPO7JFXͷηϧ
େݩͷCollectionView class LegacyViewController: UIViewController { func configureHierarchy() { collectionView.register(CollectionCell.self, forCellWithReuseIdentifier:
CollectionCell.reuseIdentifier) ɹɹɹ collectionView.collectionViewLayout = createLayout() } func createLayout() -> UICollectionViewLayout { let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width: view.bounds.width, height: view.bounds.height * 0.3) return layout } } extension LegacyViewController: UICollectionViewDataSource { } ηϧΛొɾϨΠΞτΛηοτ
ࢠͷCollectionView class SectionCollectionView: UICollectionView { override init(frame: CGRect, collectionViewLayout layout:
UICollectionViewLayout) { super.init(frame: frame, collectionViewLayout: layout) self.collectionViewLayout = createLayout() self.register(TextCell.self, forCellWithReuseIdentifier: TextCell.reuseIdentifier) self.contentInset = .init(top: 20, left: 20, bottom: 20, right: 20) } func createLayout() -> UICollectionViewLayout { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontalɹ/// εΫϩʔϧͷ͖Λԣ͖ʹ͢Δ layout.itemSize = CGSize(width: self.frame.width / 2, height: self.bounds.height - 40) return layout } } extension SectionCollectionView: UICollectionViewDataSource { }
େݩͷCollectionViewͷηϧ class CollectionCell: UICollectionViewCell { static let reuseIdentifier = "collectionCell"
var collectionView: SectionCollectionView! override init(frame: CGRect) { super.init(frame: frame) configure() } func configure() { self.collectionView = SectionCollectionView(frame: self.bounds, collectionViewLayout: .init()) contentView.addSubview(collectionView) collectionView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), collectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), collectionView.topAnchor.constraint(equalTo: contentView.topAnchor), collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) self.collectionView.backgroundColor = .white } } ηϧʹ$PMMFDUJPO7JFXΛηοτ
ࢠͷCollectionViewͷηϧ class TextCell: UICollectionViewCell { let label = UILabel() override
init(frame: CGRect) { super.init(frame: frame) configure() } } extension TextCell { func configure() { label.translatesAutoresizingMaskIntoConstraints = false label.adjustsFontForContentSizeCategory = true contentView.addSubview(label) label.font = UIFont.preferredFont(forTextStyle: .caption1) ... } }
ैདྷͷํ๏ͰαϯϓϧΞϓϦͷ UIΛ࡞͢ΔͨΊʹ ωετͨ͠CollectionView͕ඞཁ ίʔυ͕ଟ͘ͳΓɺ͔ͭߏ͕௫Έʹ͍͘
Compositional LayoutsΛ ༻ͨ͠߹
࣮ํ $PMMFDUJPO7JFX $PMMFDUJPO7JFXͷηϧ
CollectionView class ViewController: UIViewController { var collectionView: UICollectionView! override func
viewDidLoad() { super.viewDidLoad() configureHierarchy() } func configureHierarchy() { collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout()) collectionView.register(TextCell.self, forCellWithReuseIdentifier: TextCell.reuseIdentifier) collectionView.dataSource = self view.addSubview(collectionView) } func createLayout() -> UICollectionViewLayout { let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize) item.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(0.3)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20) section.orthogonalScrollingBehavior = .continuous let layout = UICollectionViewCompositionalLayout(section: section) return layout } }
CollectionViewͷηϧ class TextCell: UICollectionViewCell { let label = UILabel() override
init(frame: CGRect) { super.init(frame: frame) configure() } } extension TextCell { func configure() { label.translatesAutoresizingMaskIntoConstraints = false label.adjustsFontForContentSizeCategory = true contentView.addSubview(label) label.font = UIFont.preferredFont(forTextStyle: .caption1) ... } }
Compositional Layouts Λ ༻͢Δ͜ͱͰ̍ͭͷCollectionView Ͱදݱ͢Δ͜ͱ͕Մೳʹͳͬͨ
·ͱΊ • Compositional LayoutsΛ͏͜ͱͰࠓ·Ͱ͔ͬͨ͠ UIͷදݱ͕؆୯ʹͰ͖Δ • ࠷ۙॳΊͯiOSΞϓϦ։ൃΛ࢝ΊͨͬͯਓʹΦεεϝ ※ iOS13Ҏ߱Ͱ͔͑͠ͳ͍ͷʹҙ
ࢀߟͳͲ • ࠓճͷαϯϓϧΞϓϦͷϦϙδτϦ https://github.com/ry-itto/CompositionalLayoutsSample • Advances in Collection View Layout(ηογϣϯ)
https://developer.apple.com/videos/play/wwdc2019/215/ • ࣌ͷมԽʹԠͯ͡ਐԽ͢ΔCollectionView ~Compositional LayoutsͱDiffable Data Sources~ https://qiita.com/shiz/items/a6032543a237bf2e1d19
͋Γ͕ͱ͏͍͟͝·ͨ͠