822 Followers
·
Follow

You don’t need a third-party library for this

spinning wheel of bright neon lights set above a neon sign
spinning wheel of bright neon lights set above a neon sign
Photo by Kinson Leung on Unsplash

In this tutorial, we will learn how to implement a circular loading indicator in Swift. By the end, you will have a reusable UI element you can use in your apps to suit your needs.

In short, this is what we will master in this article:

  • Create a circular shape and path using CAShapeLayer and UIBezierPath
  • Animate circular drawing using CABasicAnimation and CAKeyframeAnimation
  • Combine several animations using CAAnimationGroup

This is the result we are going to achieve:

mobile phone screen with spinning circle that changes color from red to green to blue
mobile phone screen with spinning circle that changes color from red to green to blue

The source code of the finished project is available at the bottom of the article.

Without further ado, let’s get started.

Let’s Start

First, we start by creating a UIView subclass called…


Leverage a built-in caching mechanism

Image for post
Image for post
Photo by Scott Webb on Unsplash.

Today, we will learn how to use NSCache in Swift to cache images inside a UICollectionView.

In short, this is what you will master in this tutorial:

  • Understanding what an NSCache is and how to use it.
  • Creating a centered UICollectionView layout.
  • Executing image-loading tasks on a background thread.
  • Understanding why it is better to use an NSCache instead of a plain Dictionary when we want to cache heavy objects.

This is what we will have at the end of this article:

Image for post
Image for post

The full source code of the project is available at the bottom of the article.

Without further ado, let’s get started. …


Set the max number of operations to be executed at one time

Image for post
Image for post
Photo by Kevin Crosby on Unsplash.

In this article, we will learn how to limit the number of ongoing network requests using a DispatchSemaphore.

Limiting resource-heavy operations allows us to manage thread resources more efficiently. For example, when you have a task to simultaneously fetch ten high-quality images, it is useful to allow the execution of only one or two image loading operations at a time.

In short, this is what you will master in this tutorial:

  • Executing tasks on a background DispatchQueue.
  • Leveraging a DispatchSemaphore and setting the max number of concurrent operations.
  • Understanding the difference between using sync and async executions.

Let’s Start

First, let’s create a URL, a background DispatchQueue, and a DispatchSemaphore with a value of…

About

Zafar Ivaev

 WWDC20 Scholar. I write about features of Swift, iOS development practices, and sometimes Flutter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store