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
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
CSC307 Lecture 17
javiergs
PRO
0
320
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.3k
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
110
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
720
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
150
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
400
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Chasing Engaging Ingredients in Design
codingconduct
0
220
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Mind Mapping
helmedeiros
PRO
1
260
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Between Models and Reality
mayunak
4
340
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠