Carousel - Swift
- Infinite scrolling
- Automatic scrolling
- SwiftUI support
itemSize: CGSize - Size of each item in the carousel. Default is CollectionViewSizeinterItemSpacing: CGFloat - Spacing between carousel items. Default is 0isInfinite: Bool - Whether to enable infinite carousel. Default is falseautoScrollInterval: TimeInterval - Setting a value greater than 0 will automatically scroll the carousel at that interval in seconds. Default is 0
e.g
carousel.itemSize = CGSize(width: UIScreen.main.bounds.width * 0.80, height: 300)
carousel.interItemSpacing = 8.0
carousel.isInfinite = true
carousel.autoScrollInterval = 3.0Import ImageCarousel to use.
import ImageCarousel
let carousel = ImageCarousel()
carousel.dataSource = self
carousel.delegate = self
// Register CollectionViewCell containing the ImageView you want to display
carousel.register(ImageCarouselCell.self, forCellWithReuseIdentifier: "cell")
view.addSubview(carousel) func numberOfItems(in imageCarousel: ImageCarousel) -> Int {
carouselImages.count
}
func imageCarousel(_ imageCarousel: ImageCarousel, cellForItemAt index: Int) -> UICollectionViewCell {
let cell = carousel.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! ImageCarouselCell
cell.imageView?.image = carouselImages[index].image
return cell
} func imageCarousel(_ imageCarousel: ImageCarousel, didSelectItemAt index: Int)Delegate method called when an item is tapped
func imageCarousel(_ imageCarousel: ImageCarousel, didChangeItemAt index: Int)Delegate method called when an item changes
Import ImageCarousel to use.
import ImageCarousel
private let data = ["1", "2", "3"]
var body: some View {
Carousel(data) { element in
Image(element)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(8.0)
}
.isInfinite(true) // Whether to enable infinite carousel
.itemSize(CGSize(width: geometry.size.width * 0.80, height: geometry.size.height)) // itemSize
.interItemSpacing(8.0) // Spacing between items
.autoScrollInterval(3.0) // Auto-scroll interval
}Edit your Package.swift to install.
let package = Package(
dependencies: [
.package(url: "https://github.com/Yudai-ASANO/ImageCarousel", from: "0.1.0"),
],
targets: [
.target(
name: "<your-target-name>",
dependencies: ["ImageCarousel"]),
]
)