We are using an activity indicator in the project in order to show the progress of API calls, data store, data transfer, data delete. usually, we use a third-party library like MBProgressView to show activity indicators in the application. let’s see How to create a custom activity indicator in Swift/iOS?
Sometimes, the client or UX designer may ask for a custom-designed activity indicator to integrate in-app. Custom activity indicators could be based on their company profile like logo, color code, and other aspects.

Create custom activity indicator is a primitive task in the early stage of the project, we need to have few resources like image icon and color from the designer, for example, let’s see how we can create custom activity indicator in swift.
This is how our custom progress bar will look like.

- Let’s start by creating a new project in Xcode.
- Add a circular view image for rotate circular in the way we want, you can try these icons.



Add images to bundle:
- Select Assets.xcassets from project bundle.
- Create a new image set in Assets.xcassets.
- Add those images into Assets.xcassets file in the bundle.

Design ViewController:
Now, in ViewController take an UIImageView from the component list, and then add center horizontal and vertical constraints for UIImageView.
Add two UIButtons for start and stop activity indicator.

Code :
Add outlets of imageView in viewController file and set image to imageView by didSet property lifecycle object.
Add one timer property for handle activity indicator animation.
1 2 3 4 5 6 7 |
@IBOutlet weak var customIndicatorImageView: UIImageView! { didSet { customIndicatorImageView.image = UIImage(named: "circularImage") } } var timer: Timer? |
In viewDidLoad() hide customIndicatorImageView.
1 2 3 4 5 |
override func viewDidLoad() { super.viewDidLoad() self.customIndicatorImageView.isHidden = true } |
Now the important section of code will come around, how we can animate an image in circular form on view controller with help of timer.
- First of all, change the isHidden property of customIndicatorImageView to false.
- In this function, we are initializing timer property with scheduling 0.0, the time interval
- Here we are also calling a self.animateView() function to refer action of timer.
- Note: Timer should not be nil before initialization, because we are de-initializing timer when we are stopping the activity indicator.
1 2 3 4 5 6 |
func startTimer() { self.customIndicatorImageView.isHidden = false if timer == nil { timer = Timer.scheduledTimer(timeInterval:0.0, target: self, selector: #selector(self.animateView), userInfo: nil, repeats: false) } } |
let’s see how animateView() functions work. By using UIView.animate() predefined function, we can perform animation with multiple views. UIView.animate() has two callbacks.
- Animations: This callback calls when the animation UIView.animate() function initiated.
- Completion: This callback calls when the animation finished after a specified time duration.
In Animation callback, we rotated customIndicatorImageView in order to rotate in 360 degrees by using transform UIView property.
In Completion callback we are checking if the timer is not nil, we are rescheduling timer by referring animateView() function.
1 2 3 4 5 6 7 8 9 |
@objc func animateView() { UIView.animate(withDuration: 0.8, delay: 0.0, options: .curveLinear, animations: { self.customIndicatorImageView.transform = self.customIndicatorImageView.transform.rotated(by: CGFloat(Double.pi)) }, completion: { (finished) in if self.timer != nil { self.timer = Timer.scheduledTimer(timeInterval:0.0, target: self, selector: #selector(self.animateView), userInfo: nil, repeats: false) } }) } |
Here stopTimer() function invalidate timer and set nil to stop animation.
1 2 3 4 |
func stopTimer() { timer?.invalidate() timer = nil } |
Create IBAction Outlets of Start and Stop Button, to start and stop animation,
- On tap StartCustomIndicatorButton show customIndicatorImageView on viewController and Call startTimer() function.
- On tap StopCustomIndicatorButton hide customIndicatorImageView on viewController and Call stopTimer() function.
1 2 3 4 5 6 7 8 |
@IBAction func stopCustomIndicator(_ sender: Any) { self.customIndicatorImageView.isHidden = true stopTimer() } @IBAction func startCustomIndicator(_ sender: Any) { self.customIndicatorImageView.isHidden = false startTimer() } |
If you like this article please share. if you want to share something, please email me on blogswithrahul@gmail.com
Happy Coding!