How to Show Loading Animating Dots in Swift – IOS

In this Swift tutorial, I am going to share a simple method to show loading animating dots in Swift IOS. Using the following function you can show animating dots in a simple UIImageView. The following screenshot shows the animation that we are going to show in a UIImageView.

Show loading animating dots in swift

Method to Show Loading Animating Dots in Swift

func showAnimatingDotsInImageView() {
        let lay = CAReplicatorLayer()
        lay.frame = CGRect(x: 0, y: 8, width: 15, height: 7) //yPos == 12
        let circle = CALayer()
        circle.frame = CGRect(x: 0, y: 0, width: 7, height: 7)
        circle.cornerRadius = circle.frame.width / 2
        circle.backgroundColor = UIColor(red: 110/255.0, green: 110/255.0, blue: 110/255.0, alpha: 1).cgColor//lightGray.cgColor //
        lay.instanceCount = 3
        lay.instanceTransform = CATransform3DMakeTranslation(10, 0, 0)
        let anim = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
        anim.fromValue = 1.0
        anim.toValue = 0.2
        anim.duration = 1
        anim.repeatCount = .infinity
        circle.add(anim, forKey: nil)
        lay.instanceDelay = anim.duration / Double(lay.instanceCount)

This method generates animating dots at runtime and set it into UIImageView. It creates a CAReplicatorLayer object and then adds rounded CGRect in that object as a sub-layer. Then it defines a translation animation using CABasicAnimation and adds some attributes to set the animation as per the needs. In the end, we are assigning those animating dots to the ImageView object as a sub-layer. Don’t forget to replace the ImageView reference at the last line of code with your ImageView object.

If you have any questions feel free to ask in the comments section below.


Method to Convert Hexadecimal Color Code into UIColor Swift 5

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

One thought on “How to Show Loading Animating Dots in Swift – IOS

  1. It’s a nice and simple way. It would be nice if you make some commits or explain whate are you doing line per line. For example if I want to space more the dots or something like that.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: