Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
Set transparentAttributeWhenCellNotLoaded to false by default and a…
Browse files Browse the repository at this point in the history
…dd a SwiftUI modifier for that
  • Loading branch information
amirdew committed Jun 13, 2021
1 parent 13af82e commit 0054f7d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Lib/CollectionViewPagingLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
/// See `ZPositionHandler` for details
public var zPositionHandler: ZPositionHandler = .both

/// Set `alpha` to zero when the cell is not loaded yet by collection view
public var transparentAttributeWhenCellNotLoaded: Bool = true
/// Set `alpha` to zero when the cell is not loaded yet by collection view, enabling this prevents showing a cell before applying transforms but may cause flashing when you reload the data
public var transparentAttributeWhenCellNotLoaded: Bool = false

/// The animator for setting `contentOffset`
///
Expand Down
18 changes: 7 additions & 11 deletions Lib/SwiftUI/PagingCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent
// MARK: Internal functions

func update(list: [ValueType], currentIndex: Int?) {
if needsToReloadData(list: list) {
collectionView.performBatchUpdates({
collectionView.reloadData()
layout.invalidateLayoutInBatchUpdate()
})
}
let oldIds = self.list.map(\.id)
self.list = list
if list.map(\.id) != oldIds {
collectionView?.reloadData()
layout.invalidateLayoutInBatchUpdate(invalidateOffset: true)
}
let index = currentIndex ?? layout.currentPage
if index < list.count {
guard index != layout.currentPage else { return }
Expand All @@ -88,11 +87,7 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent


// MARK: Private functions

private func needsToReloadData(list: [ValueType]) -> Bool {
self.list.map(\.id) != list.map(\.id)
}


private func setupCollectionView() {
collectionView = UICollectionView(
frame: view.frame,
Expand All @@ -106,6 +101,7 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent
layout.numberOfVisibleItems = modifierData?.numberOfVisibleItems
layout.scrollDirection = modifierData?.scrollDirection ?? layout.scrollDirection
layout.defaultAnimator = modifierData?.animator
layout.transparentAttributeWhenCellNotLoaded = modifierData?.transparentAttributeWhenCellNotLoaded ?? layout.transparentAttributeWhenCellNotLoaded
collectionView.delegate = self

collectionView.showsHorizontalScrollIndicator = false
Expand Down
1 change: 1 addition & 0 deletions Lib/SwiftUI/PagingCollectionViewModifierData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct PagingCollectionViewModifierData {
var onTapPage: ((Int) -> Void)?
var scrollDirection: UICollectionView.ScrollDirection?
var pagePadding: PagePadding?
var transparentAttributeWhenCellNotLoaded: Bool?
}

@available(iOS 13.0, *)
Expand Down
5 changes: 5 additions & 0 deletions Lib/SwiftUI/TransformPageViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public extension TransformPageViewProtocol {
return self
}

func hideCellWhenNotLoaded(_ value: Bool) -> Self {
self.builder.modifierData.transparentAttributeWhenCellNotLoaded = value
return self
}

func collectionView<T>(_ key: WritableKeyPath<UICollectionView, T>, _ value: T) -> Self {
let property = CollectionViewProperty(keyPath: key, value: value)
self.builder.modifierData.collectionViewProperties.append(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CardsViewController: UIViewController, NibBased, ViewModelBased {
collectionView.dataSource = self
layout.numberOfVisibleItems = 7
layout.scrollDirection = .vertical
layout.transparentAttributeWhenCellNotLoaded = true
collectionView.collectionViewLayout = layout
collectionView.showsVerticalScrollIndicator = false
collectionView.clipsToBounds = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class ShapesViewController: UIViewController, NibBased, ViewModelBased {
collectionView.isPagingEnabled = true
collectionView.dataSource = self
let layout = CollectionViewPagingLayout()
layout.transparentAttributeWhenCellNotLoaded = false
collectionView.collectionViewLayout = layout
layout.delegate = self
collectionView.showsHorizontalScrollIndicator = false
Expand Down

0 comments on commit 0054f7d

Please sign in to comment.