VHCarrouselCollectionView

open class VHCarrouselCollectionView : UICollectionView

CarrouselCollectionView which multiplies source to make feel it is infinitely scrolls

You can trach centered indexPath and sourceIndexPath by setting VHCarrouselCollectionView.carrouselDelegate

– Configuration:

You should call following functions in releated delegates

1) VHCarrouselCollectionView.scrollViewWillBeginDragging()

2) VHCarrouselCollectionView.scrollViewWillEndDragging(...)

3) VHCarrouselCollectionView.scrollViewDidEndScrollingAnimation()

– Usage:

Attention

You should call

  • scrollViewWillBeginDragging() in UICollectionViewDelegate.scrollViewWillBeginDragging(...)
  • scrollViewWillEndDragging(...) in UICollectionViewDelegate.scrollViewWillEndDragging(...)
  • scrollViewDidEndScrollingAnimation() in UICollectionViewDelegate.scrollViewDidEndScrollingAnimation(...)

While using indexPath to get your related item from your source list, you should normalize it because this collectionView multiplies the indexPath.

Ex:

let source: [UIColor] = [.purple, .green]
func collectionView(
    _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! LocalCVC
    let sourceIndex = indexPath.vhCarrouselSourceIndexPath(for: collectionView).item
    cell.backgroundColor = source[sourceIndex]
    return cell
}

Attention

This collectionView has some limitations:

  • Only Horizontal direction supported
  • Only one section supported
  • Layout should be UICollectionViewFlowLayout
  • All cell sizes should be same
  • Supplementary elements are not supported (Ex: Header, Footer)
  • The object that acts as the delegate of the collection view.

    The delegate must adopt the VHCarrouselCollectionViewDelegate protocol. The delegate object is responsible for informing centered item.

    Declaration

    Swift

    open weak var carrouselDelegate: VHCarrouselCollectionViewDelegate?
  • The object that provides the data for the collection view.

    The data source must adopt the UICollectionViewDataSource protocol. The collection view maintains a weak reference to the data source object.

    Attention

    When you set dataSoruce, property value will be changed and funneled to your functions

    Declaration

    Swift

    open override var dataSource: UICollectionViewDataSource? { get set }
  • Centered source indexPath. You can safely use this indexPath while you using for source operations.

    Declaration

    Swift

    open var centeredSourceIndexPath: IndexPath? { get set }
  • Centered indexPath which can be used for view operations. You should not use it for source operations

    Attention

    When you want to indexPath for source operations you should use centeredSourceIndexPath.

    Declaration

    Swift

    open var centeredIndexPath: IndexPath? { get set }
  • Source multiplier logic

    When number of item is 1, this property ignored and used .single instead.

    Declaration

    Swift

    open var sourceMultiplier: SourceMultiplier { get set }
  • Creates a collection view object with the specified frame and layout.

    Declaration

    Swift

    public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)

    Parameters

    frame

    The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.

    layout

    The layout should be an UICollectionViewFlowLayout

  • View init coder

    Declaration

    Swift

    public required init?(coder: NSCoder)
  • Tells the view that its window object changed.

    Declaration

    Swift

    open override func didMoveToWindow()
  • Lays out subviews.

    Declaration

    Swift

    open override func layoutSubviews()
  • Reloads all of the data for the collection view.

    This will center first source item.

    When you do not want to change scroll position you can call reloadData(scrollToInitial: false)

    Declaration

    Swift

    open override func reloadData()
  • Scrolls the collection view contents until the specified item is visible.

    Attention

    If animated is false, collection view may not infinitely scroll.

    If you want to correctly scroll use scrollToSource(...) instead.

    Declaration

    Swift

    open override func scrollToItem(
        at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool
    )

    Parameters

    indexPath

    The index path of the item to scroll into view.

    scrollPosition

    An option that specifies where the item should be positioned when scrolling finishes. For a list of possible values, see UICollectionView.ScrollPosition.

    animated

    Specify true to animate the scrolling behavior or false to adjust the scroll view’s visible content immediately.

Scroll API

  • Scrolls the collection view contents until the specified item which multiplied with sourceMultiplier is visible.

    Declaration

    Swift

    func scrollToSource(indexPath: IndexPath, animated: Bool)

    Parameters

    indexPath

    The index path of the source item to scroll into view.

    animated

    Specify true to animate the scrolling behavior or false to adjust the scroll view’s visible content immediately.

  • Scrolls to next item in view

    Declaration

    Swift

    func scrollToNext(animated: Bool = true)

    Parameters

    animated

    Scrolling animated if true. Default value is true.

  • Scrolls to previous item in view

    Declaration

    Swift

    func scrollToPrevious(animated: Bool = true)

    Parameters

    animated

    Scrolling animated if true. Default value is true.

  • Source Multiplier Logic

    See more

    Declaration

    Swift

    enum SourceMultiplier

Timer API

  • Setup timer to scroll next item in view periodically

    Attention

    - You should call enableTimer() when we is ready

    Declaration

    Swift

    func configureTimer(interval: TimeInterval, animated: Bool)

    Parameters

    interval

    Timer interval in seconds. Default value is 3.

    animated

    Is scrolling to next item will be animated. Default value is true.

  • Enables timer to scroll next item in view periodically.

    Declaration

    Swift

    func enableTimer()
  • Disables timer to scroll next item in view periodically.

    Declaration

    Swift

    func disableTimer()

Should Called API in UICollectionViewDelegate

  • Should be called in UICollectionViewDelegate.scrollViewWillBeginDragging(...)

    Declaration

    Swift

    func scrollViewWillBeginDragging()
  • Should be called in UICollectionViewDelegate.scrollViewWillEndDragging(...)

    Declaration

    Swift

    func scrollViewWillEndDragging(with velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
  • Should be called in UICollectionViewDelegate.scrollViewDidEndScrollingAnimation(...)

    Declaration

    Swift

    func scrollViewDidEndScrollingAnimation()

Public API

  • Reloads all of the data for the collection view.

    Declaration

    Swift

    func reloadData(scrollToInitial: Bool)

    Parameters

    scrollToInitial

    If value is true, first source item will be centered in view.