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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ry-itto
November 02, 2019
Programming
79
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
540
Composable Architecture
ryitto
0
810
Swift5.1 SwiftUI
ryitto
0
140
Other Decks in Programming
See All in Programming
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
130
symfony/aiとlaravel/boost
77web
0
100
Oxcを導入して開発体験が向上した話
yug1224
4
350
Contextとはなにか
chiroruxx
1
380
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
160
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
220
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
6k
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.8k
dRuby over BLE
makicamel
2
400
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
330
Featured
See All Featured
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
150
How to Talk to Developers About Accessibility
jct
2
270
エンジニアに許された特別な時間の終わり
watany
107
250k
Mind Mapping
helmedeiros
PRO
1
270
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
RailsConf 2023
tenderlove
30
1.5k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
380
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
330
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
240
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠