EDCarousel
is a Custom collection view Layout
library for overlapping style carousel collection view flow layout.
- Animate cell scale while scrolling
- Easy to integrate and use
- Easy Customizeable
- Page control
To install using Cocoapods, clone this repo and go to Example directory then run the following command:
pod install
https://github.com/emrdgrmnci/EDCarousel/tree/main/Example
Set the UICollectionView layout class to CarouselFlowLayout as given below.
- 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
}
scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
method use to scroll specific index.
collectionView.scrollToItem(
at: indexPath,
at: .centeredHorizontally,
animated: true
)
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.
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.
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.
EDCarousel
is primarily the work of Emre Degirmenci.