TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to get a Cover Flow effect on iOS

Swift version: 5.6

Paul Hudson    @twostraws   

You can get an instant Cover Flow effect on iOS by using the marvelous and free iCarousel library. You can download it from https://github.com/nicklockwood/iCarousel and drop it into your Xcode project fairly easily by adding a bridging header (it's written in Objective-C).

If you haven't added Objective-C code to a Swift project before, follow these steps:

  • Download iCarousel and unzip it
  • Go into the folder you unzipped, open its iCarousel subfolder, then select iCarousel.h and iCarousel.m and drag them into your project navigation – that's the left pane in Xcode. Just below Info.plist is fine.
  • Check "Copy items if needed" then click Finish.
  • Xcode will prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Create Bridging Header"
  • You should see a new file in your project, named YourProjectName-Bridging-Header.h.
  • Add this line to the file: #import "iCarousel.h"

Once you've added iCarousel to your project you can start using it. Make sure you conform to both the iCarouselDelegate and iCarouselDataSource protocols.

Here's a complete, albeit simplified, example:

override func viewDidLoad() {
    super.viewDidLoad()

    let carousel = iCarousel(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
    carousel.dataSource = self
    carousel.type = .coverFlow
    view.addSubview(carousel)

}

func numberOfItems(in carousel: iCarousel) -> Int {
    return 10
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    let imageView: UIImageView

    if view != nil {
        imageView = view as! UIImageView
    } else {
        imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 128, height: 128))
    }

    imageView.image = UIImage(named: "example")

    return imageView
}

That example loads the same image for all 10 carousel slides, so you'll need to change that to load data from your app.

If you have the time, do check out the other carousel types that iCarousel offers – they're quite remarkable!

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 5.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.1/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.