Skip to content

emrdgrmnci/EDCarousel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EDCarousel

EDCarousel is a Custom collection view Layout library for overlapping style carousel collection view flow layout.

Features

  • Animate cell scale while scrolling
  • Easy to integrate and use
  • Easy Customizeable
  • Page control

Installation

To install using Cocoapods, clone this repo and go to Example directory then run the following command:

pod install

Example

https://github.com/emrdgrmnci/EDCarousel/tree/main/Example

Visual

Usage

via Interface Builder

Set the UICollectionView layout class to CarouselFlowLayout as given below.

Alt text

Detect current indexPath or page while scrolling

  • After scrolling end with left or right then you can use the scrollViewWillEndDragging delegate method. This method called when scroll view grinds to a halt.
func scrollViewWillEndDragging(
        _: UIScrollView,
        withVelocity _: CGPoint,
        targetContentOffset: UnsafeMutablePointer<CGPoint>
    ) {
        let targetOffset = targetContentOffset.pointee.x
        let width = (collectionView.frame.size.width - padding) / 1.21
        let rounded = Double((images.count / 2)) * abs(targetOffset / width)
        let scale = round(rounded)
        pageControl.currentPage = Int(scale)
        updateButtonStates(with: pageControl.currentPage)
        updateUI(with: pageControl.currentPage)
    }
  • Updates the button and page control states.
//
@IBAction
    func didTapOnPreviousButton(_: Any) {
        let prevIndex = max(pageControl.currentPage - 1, 0)
        let indexPath = IndexPath(item: prevIndex, section: 0)
        pageControl.currentPage = prevIndex
        collectionView?.isPagingEnabled = false
        collectionView.scrollToItem(
            at: indexPath,
            at: .centeredHorizontally,
            animated: true
        )
        updateButtonStates(with: pageControl.currentPage)
        updateUI(with: pageControl.currentPage)
    }

    @IBAction
    func didTapOnNextButton(_: Any) {
        let nextIndex = min(pageControl.currentPage + 1, images.count - 1)
        let indexPath = IndexPath(item: nextIndex, section: 0)
        pageControl.currentPage = nextIndex
        collectionView?.isPagingEnabled = false
        collectionView.scrollToItem(
            at: indexPath,
            at: .centeredHorizontally,
            animated: true
        )
        updateButtonStates(with: pageControl.currentPage)
        updateUI(with: pageControl.currentPage)
    }

    private func updateUI(with currentPage: Int) {
        pageControl.currentPage = currentPage
        pageControl.indicatorImage(forPage: pageControl.currentPage)
        previousButton.isHidden = currentPage == 0
        nextButton.isHidden = currentPage == images.count - 1
        if currentPage == images.count - 1 {
            nextButton.isHidden = false
            nextButton.setImage(UIImage(), for: .normal)
        } else {
            nextButton.setImage(UIImage("image"), for: .normal)
        }
    }

    private func updateButtonStates(with currentPage: Int) {
        pageControl.currentPage = currentPage
        previousButton.isEnabled = currentPage > 0
        nextButton.isEnabled = currentPage < images.count
    }

Scroll to specifc index

scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) method use to scroll specific index.

collectionView.scrollToItem(
            at: indexPath,
            at: .centeredHorizontally,
            animated: true
        )

UICollectionView Extension Usage Example

Here's an example of how you can use these extension methods:

// Register cell class
collectionView.register(MyCell.self)

// Register cell with nib file
collectionView.registerNib(MyNibCell.self, bundle: nil)

// Dequeue cell
if let cell = collectionView.dequeue(MyCell.self, for: indexPath) {
    // Configure and use the dequeued cell
} else {
    // Handle the case where the dequeued cell is not of the expected class
}

In the example above, MyCell and MyNibCell represent the cell classes you ßwant to register and dequeue in the collection view.

Conclusion

The UICollectionView extension provides convenient methods for registering and dequeuing cells in a collection view. These methods simplify the process of working with collection view cells, making it easier to manage and reuse cells in your application.

Contributing

Feel free to open an issue if you have questions about how to use EDCarousel, discovered a bug, or want to improve the implementation or interface.

Credits

EDCarousel is primarily the work of Emre Degirmenci.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published