Upgrade to Pro — share decks privately, control downloads, hide ads and more …

時代の変化に応じて進化するCollectionView

 時代の変化に応じて進化するCollectionView

iOS13で新しく導入されたCollectionViewについての内容です。

[email protected]

June 28, 2019
Tweet

Other Decks in Programming

Transcript

  1. QFSGPSN#BUDI6QEBUFT // Animate all other update types together. collectionView.performBatchUpdates({ var

    deletes = [Int]() var inserts = [(person:Person, index:Int)]() for update in remoteUpdates { switch update { case let .delete(index): collectionView.deleteItems(at: [IndexPath(item: index, section: 0)]) deletes.append(index) case let .insert(person, index): collectionView.insertItems(at: [IndexPath(item: index, section: 0)]) inserts.append((person, index)) case let .move(fromIndex, toIndex): // Updates that move a person are split into an addition and a deletion. collectionView.moveItem(at: IndexPath(item: fromIndex, section: 0), to: IndexPath(item: toIndex, section: 0)) deletes.append(fromIndex) inserts.append((people[fromIndex], toIndex)) default: break } } // Apply deletions in descending order. for deletedIndex in deletes.sorted().reversed() { people.remove(at: deletedIndex) } // Apply insertions in ascending order. let sortedInserts = inserts.sorted(by: { (personA, personB) -> Bool in return personA.index <= personB.index }) for insertion in sortedInserts { people.insert(insertion.person, at: insertion.index) } // The update button is enabled only if the list still has people in it. navigationItem.rightBarButtonItem?.isEnabled = !people.isEmpty }) IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOVJLJUVJDPMMFDUJPOWJFXDVTUPNJ[JOH@DPMMFDUJPO@WJFX@MBZPVUT DIBOHFTMBUFTU@NJOPS ͜ͷॱ൪Ͱͳ͍ͱ /4*ODPOTJTUFODZ&YDFQUJPO
  2. class NSCollectionLayoutDimension { class func fractionalWidth(_ fractionalWidth: CGFloat) -> Self

    class func fractionalHeight(_ fractionalHeight: CGFloat) -> Self class func absolute(_ absoluteDimension: CGFloat) -> Self class func estimated(_ estimatedDimension: CGFloat) -> Self } ਌ͷίϯϙʔωϯτͷ αΠζʹର͢Δׂ߹Ͱࢦఆ ϙΠϯτͰࢦఆ /4$PMMFDUJPO-BZPVU4J[F https://developer.apple.com/documentation/uikit/nscollectionlayoutsize
  3. private func createLayout() -> UICollectionViewLayout { let itemSize = NSCollectionLayoutSize(widthDimension:

    .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(44)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) let layout = UICollectionViewCompositionalLayout(section: section) return layout } ᶃ4J[FΛܾΊΔ -JTU7JFX$POUSPMMFS ᶄ4J[FΛࢦఆͯ͠*UFNΛ࡞Δ ᶅ4J[Fͱ*UFNΛࢦఆͯ͠(SPVQΛ࡞੒ ᶆ(SPVQΛࢦఆͯ͠4FDUJPOΛ࡞੒ ᶇ4FDUJPOΛࢦఆͯ͠-BZPVUΛ࡞੒
  4. private func createLayout() -> UICollectionViewLayout { let itemSize = NSCollectionLayoutSize(

    widthDimension: .fractionalWidth(0.2), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize) // add for each item allocate same box item.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(0.2)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) let layout = UICollectionViewCompositionalLayout(section: section) return layout } ̍ͭͷάϧʔϓʹॎ͸ˋɺԣ͸ˋͷαΠζͰ*UFNΛฒ΂Δ (SJE7JFX$POUSPMMFS
  5. func createLayout() -> UICollectionViewLayout { let layout = UICollectionViewCompositionalLayout {

    (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in guard let sectionLayoutKind = SectionLayoutKind(rawValue: sectionIndex) else { return nil } let columns = sectionLayoutKind.columnCount // The `group` auto-calculates the actual item width to make // the requested number of `columns` fit, so this `widthDimension` will be ignored. let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize) item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2) let groupHeight = columns == 1 ? NSCollectionLayoutDimension.absolute(44) : NSCollectionLayoutDimension.fractionalWidth(0.2) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: groupHeight) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: columns) let section = NSCollectionLayoutSection(group: group) section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20) return section } return layout } 4FDUJPOʹԠͯ͡ΧϥϜ਺Λมߋ DistinctSectionsViewController
  6. let badgeAnchor = NSCollectionLayoutAnchor( edges: [.top, .trailing], fractionalOffset: CGPoint(x: 0.3,

    y: -0.3)) let badgeSize = NSCollectionLayoutSize(widthDimension: .absolute(20), heightDimension: .absolute(20)) let badge = NSCollectionLayoutSupplementaryItem( layoutSize: badgeSize, elementKind: ViewController.badgeElementKind, containerAnchor: badgeAnchor) let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.25), heightDimension: .fractionalHeight(1.0)) let item = NSCollectionLayoutItem(layoutSize: itemSize, supplementaryItems: [badge]) /4$PMMFDUJPO-BZPVU"ODIPS Ͳ͜ʹόονΛஔ͘ͷ͔ΛܾΊ͍ͯΔ ItemBadgeSupplementaryViewController "badge-element-kind" ϔομʔ΍ϑολʔ͸ NSCollectionLayoutBoundarySupplementaryItem Itemʹࢦఆ
  7. func createLayout() -> UICollectionViewLayout { let layout = UICollectionViewCompositionalLayout(sectionProvider: {

    (sectionIndex:Int, layoutEnvironment:NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in let leadingItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize( widthDimension: .fractionalWidth(0.7), heightDimension: .fractionalHeight(1.0))) leadingItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10) let trailingItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize( widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(0.3))) trailingItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10) let trailingGroup = NSCollectionLayoutGroup.vertical(layoutSize: NSCollectionLayoutSize( widthDimension: .fractionalWidth(0.3), heightDimension: .fractionalHeight(1.0)), subitem: trailingItem, count: 2) let containerGroupFractionalWidth = orthogonallyScrolls ? CGFloat(0.85) : CGFloat(1.0) let groupSize = NSCollectionLayoutGroup.horizontal( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(containerGroupFractionalWidth), heightDimension: .fractionalHeight(0.4)) let containerGroup = NSCollectionLayoutGroup.horizontal( layoutSize: groupSize, subitems: [leadingItem, trailingGroup]) let section = NSCollectionLayoutSection(group: containerGroup) section.orthogonalScrollingBehavior = orthogonalScrollingBehavior() return section }, configuration: config) return layout } (SPVQͷதʹ(SPVQΛೖΕΔ OrthogonalScrollingViewController ͜ΕߦͰωετ͞Εͨ(SPVQͷεΫϩʔϧͷಈ͖Λௐ੔Ͱ͖Δ
  8. 6*ͷߋ৽ εςοϓ func performQuery(with filter: String?) { let mountains =

    mountainsController .filteredMountains(with: filter) .sorted { $0.name < $1.name } let snapshot = NSDiffableDataSourceSnapshot<Section, Mountain>() snapshot.appendSections([.main]) snapshot.appendItems(mountains) dataSource.apply(snapshot, animatingDifferences: true) } ͭͷεςοϓΛ౿Ή͚ͩͰઃఆ͕Ͱ͖Δ .PVOUBJOT7JFX$POUSPMMFS 1. ৽͍͠Snapshotͷੜ੒ 2. ߋ৽͍ͨ͠৘ใͷ௥Ճ 3. applyͷݺͼग़͠
  9. 4FDUJPO *UFN struct Mountain: Hashable { let name: String let

    height: Int let identifier = UUID() func hash(into hasher: inout Hasher) { hasher.combine(identifier) } static func == (lhs: Mountain, rhs: Mountain) -> Bool { return lhs.identifier == rhs.identifier } } enum Section: CaseIterable { case main } .PVOUBJOT7JFX$POUSPMMFS
  10. %BUB4PVSDFͷઃఆ func configureDataSource() { dataSource = UICollectionViewDiffableDataSource <Section, Item>(collectionView: collectionView)

    { (collectionView: UICollectionView, indexPath: IndexPath, item: Item) -> UICollectionViewCell? in guard let cell = collectionView.dequeueReusableCell( withReuseIdentifier: Cell.reuseIdentifier, for: indexPath) as? Cell else { fatalError("Cannot create new cell") } cell.label.text = item.name return cell } } *OEFY1BUIΛ࢖༻͍ͯ͠ͳ͍ ΫϩʔδϟͷதͰηϧ΁ͷ஋ͷઃఆํ๏Λએݴ
  11. ηΫγϣϯ͕ෳ਺͋Δ৔߹ func updateUI(animated: Bool = true) { guard let controller

    = self.wifiController else { return } let configItems = configurationItems.filter { !($0.type == .currentNetwork && !controller.wifiEnabled) } currentSnapshot = NSDiffableDataSourceSnapshot<Section, Item>() currentSnapshot.appendSections([.config]) currentSnapshot.appendItems(configItems, toSection: .config) if controller.wifiEnabled { let sortedNetworks = controller.availableNetworks.sorted { $0.name < $1.name } let networkItems = sortedNetworks.map { Item(network: $0) } currentSnapshot.appendSections([.networks]) currentSnapshot.appendItems(networkItems, toSection: .networks) } self.dataSource.apply(currentSnapshot, animatingDifferences: animated) } ηΫγϣϯ͝ͱʹ஋Λઃఆ͢Δ 8J'J4FUUJOHT7JFX$POUSPMMFS
  12. ඞཁͳͱ͜Ζ͚ͩߋ৽͢Δ func performSortStep() { let updatedSnapshot = dataSource.snapshot() updatedSnapshot.sectionIdentifiers.forEach {

    let section = $0 if !section.isSorted { section.sortNext() let items = section.values updatedSnapshot.deleteItems(items) updatedSnapshot.appendItems(items, toSection: section) sectionCountNeedingSort += 1 } } var shouldReset = false var delay = 125 if sectionCountNeedingSort > 0 { dataSource.apply(updatedSnapshot) } else { delay = 1000 shouldReset = true } } ݱࡏͷ4OBQTIPUͷঢ়ଶΛऔಘ ඞཁͳͱ͜Ζ͚ͩߋ৽Λ͢Δ *OEFY1BUIͷ؅ཧ͕ෆཁͳͷͰ ෆ੔߹͕ى͖Δ͜ͱ͕ͳ͍ *OTFSUJPO4PSU7JFX$POUSPMMFS