Member-only story

iOS scaling carousel component in Swift

Pete Smith
6 min readFeb 8, 2017

--

Crossposted from petethedeveloper.com

I recently published this library. It’s a custom UICollectionView component with carousel-style scrolling & scaling of cells. It’s used in Bikey, as seen below;

In this post, I describe the structure of the library, how it works, and how to use it.

Library structure

ScalingCarousel consists of 3 Swift classes, shown below in order of relative importance (because what kind of person creates an unordered list! 😜);

  1. ScalingCarouselView: A UICollectionView subclass, and the most interesting part. It does most of the heavy lifting.
  2. ScalingCarouselLayout: A UICollectionViewFlowLayout subclass, it handles sizing of cells, and collection view layout.
  3. ScalingCarouselCell: A UICollectionViewCell subclass. It provides a method to scale itself.

How it works

  • When ScalingCarouselView is initialized, it adds an ‘invisible’ UIScrollView as a subview of it’s immediate superview. We say it’s ‘invisible’ as it won’t be visible to the user, and because it also allows it’s touch events to fall through to the underlying ScalingCarouselView. This additional UIScrollView is the key to everything, as it allows our ScalingCarouselCells to…

--

--

Pete Smith
Pete Smith

Written by Pete Smith

Software Engineer @Zendesk. Occasional Medium writer. Sign up here https://superpeteblaze.medium.com/membership

Responses (12)