SwiftImageCarousel is a reusable UIPageController carousel for iOS
SwiftImageCarousel is a UIPageController-based framework that implements scrolling, zooming and automatic swiping carousel in a Swift application. All it needs from you is to supply it with valid image URLs and it is good to go.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate SwiftImageCarousel into your Xcode project using CocoaPods, specify it in your Podfile
:
target '<Your Target Project Name>' do
use_frameworks!
pod 'SwiftImageCarousel'
end
Or
import SwiftImageCarousel
SwiftImageCarousel
in a UIContainerViewController /// A UIView declared in Main.Storyboard.
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
vc.contentImageURLs = ["<Your First URL>", "<Your Second URL>", "<Your Third URL>"]
// Adding it to the container view
vc.willMove(toParentViewController: self)
containerView.addSubview(vc.view)
vc.view.frame = CGRect(x: 0, y: 0, width: containerView.frame.width, height: containerView.frame.height)
self.addChildViewController(vc)
vc.didMove(toParentViewController: self)
}
contentImageURLs
Array with image URLs provided as Strings let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// Default URLs are not provided. They need to be set up.
vc.contentImageURLs = [ "<Your First URL>", "<Your Second URL>", "<Your Third URL>"]
noImage
UIImage variable let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
// There is no default image for this variable. It needs to be set up.
vc.noImage = <Some UIImage from the Assets in your project>
isTimerOn
Bool variable let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// Timer ON/OFF boolean variable. Timer for automatic swiping is set true(or ON) by default. If it needs to be off, it needs to be set to false.
vc.isTimerOn = false
swipeTimeIntervalSeconds
Double variable let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// The interval on which the carousel swipes automatically when timer is on. Default is 3 seconds.
vc.swipeTimeIntervalSeconds = 1.5
contentMode
UIViewContentMode variable let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// Enables resetting the UIViewContentMode of SwiftImageCarouselItemVC UIViewContentMode. The default is .scaleAspectFit
vc.contentMode = .scaleToFill
SwiftImageCarouselVC
) using escapeFirstPageControlDefaultFrame
Bool varaible: let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// The default value of the variable is false
vc.escapeFirstPageControlDefaultFrame = true
showCloseButtonInModalGallery
Bool variable let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// Enables/disables the showing of the close button in the modal gallery
vc.showCloseButtonInModalGallery = true
showModalGalleryOnTap
Bool variableThis variable would usually be used in combination with the didTapSwiftImageCarouselItemVC(SwiftImageCarouselItemController: SwiftImageCarouselItemVC)
SwiftImageCarouselVCDelegate function.
let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
/// Enables/disables the showing of the modal gallery
vc.showModalGalleryOnTap = false
After you create SwiftImageCarouselVC
programatically, setup the swiftImageCarouselVCDelegate to self:
let storyboard = UIStoryboard (name: "Main", bundle: Bundle(for: SwiftImageCarouselVC.self))
let vc = storyboard.instantiateInitialViewController() as! SwiftImageCarouselVC
vc.swiftImageCarouselVCDelegate = self
SwiftImageCarouselVCDelegate
Setting showModalGalleryOnTap to false and instead performing a different segue than the default one when clicking on one of the images in the carousel.
/// Enables/disables the showing of the modal gallery
vc.showModalGalleryOnTap = false
extension ViewController: SwiftImageCarouselVCDelegate {
func didTapSwiftImageCarouselItemVC(swiftImageCarouselItemController: SwiftImageCarouselItemVC) {
// The user selected this swiftImageCarouselItemController
}
}
Setting up the appearance of the page controls (colored dots)
extension ViewController: SwiftImageCarouselVCDelegate {
func setupAppearance(forFirst firstPageControl: UIPageControl, forSecond secondPageControl: UIPageControl) {
firstPageControl.backgroundColor = .red
firstPageControl.currentPageIndicatorTintColor = .yellow
}
}
Getting the timer and its properties
extension ViewController: SwiftImageCarouselVCDelegate {
func didStartTimer(_ timer: Timer) {
print (timer.timeInterval)
}
}
Getting the next pageItemController when the timer is on
extension ViewController: SwiftImageCarouselVCDelegate {
func didGetNextITemController(next pageItemController: InitialPageItemController) {
pageItemController.view.backgroundColor = .green
}
}
Getting the pageItemController when unwinding from ScrollablePageItemController
extension ViewController: SwiftImageCarouselVCDelegate {
func didUnwindToPageViewController(unwindedTo pageItemController: InitialPageItemController) {
pageItemController.view.backgroundColor = .green
}
}
SwiftImageCarousel is released under the MIT license. See LICENSE for details.